Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ';平铺';使用numpy的二维数组_Python_Arrays_Numpy - Fatal编程技术网

Python ';平铺';使用numpy的二维数组

Python ';平铺';使用numpy的二维数组,python,arrays,numpy,Python,Arrays,Numpy,我试图通过获取数组的大部分方形块并将它们写入另一个数组来减小2D数组的大小。方形块的大小是可变的,比如说,一侧有n个值。数组的数据类型将是整数。我目前正在使用python中的一个循环将每个块分配给一个临时数组,然后从tmpArray中提取唯一的值。然后我通过这些循环找到发生率最高的一个。可以想象,随着输入数组大小的增加,这个过程很快变得太慢 我看到过一些例子,从我的方块中提取最小值、最大值和平均值,但我不知道如何将它们转换为多数。 和 我正在寻找一些方法来加速这个过程,方法是使用numpy在整

我试图通过获取数组的大部分方形块并将它们写入另一个数组来减小2D数组的大小。方形块的大小是可变的,比如说,一侧有n个值。数组的数据类型将是整数。我目前正在使用python中的一个循环将每个块分配给一个临时数组,然后从tmpArray中提取唯一的值。然后我通过这些循环找到发生率最高的一个。可以想象,随着输入数组大小的增加,这个过程很快变得太慢

我看到过一些例子,从我的方块中提取最小值、最大值和平均值,但我不知道如何将它们转换为多数。 和

我正在寻找一些方法来加速这个过程,方法是使用numpy在整个阵列上执行这个过程。(当输入变得太大而无法放入内存时,切换到阵列的平铺部分,我可以处理这方面的问题)

谢谢

#snippet of my code
#pull a tmpArray representing one square chunk of my input array
kernel = sourceDs.GetRasterBand(1).ReadAsArray(int(sourceRow), 
                                    int(sourceCol), 
                                    int(numSourcePerTarget),
                                    int(numSourcePerTarget))
#get a list of the unique values
uniques = np.unique(kernel)
curMajority = -3.40282346639e+038
for val in uniques:
    numOccurances = (array(kernel)==val).sum()
    if numOccurances > curMajority:
        ans = val
        curMajority = numOccurances

#write out our answer
outBand.WriteArray(curMajority, row, col)

#This is insanity!!!
按照Bago的优秀建议,我认为我正在找到解决方案。 这是我到目前为止所拥有的。我做的一个更改是使用原始网格形状的(xy,nn)数组。我遇到的问题是,我似乎不知道如何将where、counts和uniq__a步骤从一维转换为二维

#test data
grid = np.array([[ 37,  1,  4,  4, 6,  6,  7,  7],
                 [ 1,  37,  4,  5, 6,  7,  7,  8],
                 [ 9,  9, 11, 11, 13,  13,  15,  15],
                 [9, 10, 11, 12, 13,  14,  15,  16],
                 [ 17, 17,  19,  19, 21,  11,  23,  23],
                 [ 17, 18,  19,  20, 11,  22,  23,  24],
                 [ 25, 25, 27, 27, 29,  29,  31,  32],
                 [25, 26, 27, 28, 29,  30,  31,  32]])
print grid

n = 4
X, Y = grid.shape
x = X // n
y = Y // n
grid = grid.reshape( (x, n, y, n) )
grid = grid.transpose( [0, 2, 1, 3] )
grid = grid.reshape( (x*y, n*n) )
grid = np.sort(grid)
diff = np.empty((grid.shape[0], grid.shape[1]+1), bool)
diff[:, 0] = True
diff[:, -1] = True
diff[:, 1:-1] = grid[:, 1:] != grid[:, :-1]
where = np.where(diff)

#This is where if falls apart for me as 
#where returns two arrays:
# row indices [0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3]
# col indices [ 0  2  5  6  9 10 13 14 16  0  3  7  8 11 12 15 16  0  3  4  7  8 11 12 15
# 16  0  2  3  4  7  8 11 12 14 16]
#I'm not sure how to get a 
counts = where[:, 1:] - where[:, -1]
argmax = counts[:].argmax()
uniq_a = grid[diff[1:]]
print uniq_a[argmax]

这是一个可以更快地找到大多数的函数,它基于numpy.unique的实现

def get_majority(a):
    a = a.ravel()
    a = np.sort(a)
    diff = np.empty(len(a)+1, 'bool')
    diff[0] = True
    diff[-1] = True
    diff[1:-1] = a[1:] != a[:-1]
    where = np.where(diff)[0]
    counts = where[1:] - where[:-1]
    argmax = counts.argmax()
    uniq_a = a[diff[1:]]
    return uniq_a[argmax]
如果有帮助,请告诉我

更新

您可以执行以下操作将数组设置为
(n*n,x,y)
,这将设置您在第一个轴上操作,并以矢量化方式完成此操作

X, Y = a.shape
x = X // n
y = Y // n
a = a.reshape( (x, n, y, n) )
a = a.transpose( [1, 3, 0, 2] )
a = a.reshape( (n*n, x, y) )

只需要记住几件事。即使在可能的情况下重塑和转置返回视图,我相信重塑转置将被迫复制。同样,将上述方法推广到轴上操作也是可能的,但可能需要一些创造性。

这可能有点牵强,但我最终求助于scipy.stats.stats模式函数来查找多数值。就处理时间而言,我不确定这与其他解决方案相比如何

import scipy.stats.stats as stats
#test data
grid = np.array([[ 37,  1,  4,  4, 6,  6,  7,  7],
                 [ 1,  37,  4,  5, 6,  7,  7,  8],
                 [ 9,  9, 11, 11, 13,  13,  15,  15],
                 [9, 10, 11, 12, 13,  14,  15,  16],
                 [ 17, 17,  19,  19, 21,  11,  23,  23],
                 [ 17, 18,  19,  20, 11,  22,  23,  24],
                 [ 25, 25, 27, 27, 29,  29,  31,  32],
                 [25, 26, 27, 28, 29,  30,  31,  32]])
print grid

n = 2
X, Y = grid.shape
x = X // n
y = Y // n
grid = grid.reshape( (x, n, y, n) )
grid = grid.transpose( [0, 2, 1, 3] )
grid = grid.reshape( (x*y, n*n) )
answer =  np.array(stats.mode(grid, 1)[0]).reshape(x, y)

那当然有帮助!我仍然希望一次在整个数据集上实现这个算法。类似于grid.reforme((5,grid.shape[0]//55,-1)).max(axis=3)。max(1)将提供最大值。如果我弄明白了,我将发布解决方案。这是一个很大的帮助!看到我有多少东西需要学习,我感到很谦卑。我已经仔细考虑了你的建议,但是把它改成了(xy,nn)数组。我认为stats.mode是一个好方法。很抱歉成为ocd,但是您可以在最后一行删除np.array,因为mode返回数组。