使用NumPy对二维数组进行平铺

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

3

这里有一个函数可以更快地找到大多数值,它基于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) )

有几件事需要记住。尽管reshape和transpose在可能的情况下返回视图,但我认为reshape-transpose-reshape将被强制复制。此外,将上述方法推广到操作轴应该是可能的,但可能需要一点创意。


那肯定有帮助!我仍然希望能够一次在整个数据集上实现这个算法。例如,grid.reshape((5,grid.shape[0]//55,-1)).max(axis=3).max(1)将提供最大值。如果我想出解决方案,我会发布它的。 - Colin Talbert
太有用了!看到我还需要学习这么多,让人感到谦卑。我已经按照您的建议进行了工作,但将其更改为(x * y,n * n)数组。 - Colin Talbert

1

这可能有点投机取巧,但我最终使用了scipy.stats.stats模块的mode函数来找到众数。我不确定在处理时间方面与其他解决方案相比如何。

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)

我认为stats.mode是一个不错的选择。抱歉我有点强迫症,但你可以在最后一行去掉np.array,因为mode会返回数组。 - Bi Rico

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接