使用numpy“平铺”一个二维数组

时间:2012-01-30 22:00:43

标签: python arrays numpy

我想通过占用阵列的大部分方块并将这些块写入另一个阵列来减小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的优秀建议,我认为我正在寻求解决方案。 这是我到目前为止所拥有的。我做的一个改变是使用原始网格形状的(x y,n n)数组。我遇到的问题是我似乎无法弄清楚如何将where,count和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 个答案:

答案 0 :(得分: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将被强制复制。也可以概括上述方法在轴上操作,但可能需要一些创造力。

答案 1 :(得分:1)

这可能是一个警察,但我最终采用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)