我正在尝试通过取数组的方块块的大多数并将其写入另一个数组来缩小2D数组的大小。 方块的大小可变,假设每边有n个值。 数组的数据类型将是整数。 我目前在Python中使用循环将每个块分配给临时数组,然后从tmpArray中获取唯一值。 然后我循环遍历它们并找到出现次数最多的值。 正如您所想象的那样,随着输入数组的大小增加,这个过程很快变得太慢。
我看到了一些示例从我的方块块中获取最小值、最大值和平均值,但是我不知道如何将它们转换为大多数。 Grouping 2D numpy array in average 和 resize with averaging or rebin a numpy 2d array 我希望通过使用numpy在整个数组上执行此过程来加速此过程(随着输入变得太大而切换到数组的平铺部分,我可以处理此方面)。
谢谢
我看到了一些示例从我的方块块中获取最小值、最大值和平均值,但是我不知道如何将它们转换为大多数。 Grouping 2D numpy array in average 和 resize with averaging or rebin a numpy 2d array 我希望通过使用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]