python – numpy数组的计算平均值

我有一个2d numpy数组(6 x 6)元素.我想从中创建另一个2D数组,其中每个块是块大小窗口中所有元素的平均值.目前,我有foll.码:

import os, numpy

def avg_func(data, blocksize = 2):
    # Takes data, and averages all positive (only numerical) numbers in blocks
    dimensions = data.shape

    height = int(numpy.floor(dimensions[0]/blocksize))
    width = int(numpy.floor(dimensions[1]/blocksize))
    averaged = numpy.zeros((height, width))

    for i in range(0, height):
        print i*1.0/height
        for j in range(0, width):
            block = data[i*blocksize:(i+1)*blocksize,j*blocksize:(j+1)*blocksize]
            if block.any():
                averaged[i][j] = numpy.average(block[block>0])

    return averaged

arr = numpy.random.random((6,6))
avgd = avg_func(arr, 3)

有什么方法可以让它更加pythonic? numpy有可能已经有了它吗?

UPDATE

根据M. Massias的下面的解决方案,这里是一个更新,固定值被变量替换.不确定它是否编码正确.它确实似乎工作:

dimensions = data.shape 
height = int(numpy.floor(dimensions[0]/block_size)) 
width = int(numpy.floor(dimensions[1]/block_size)) 

t = data.reshape([height, block_size, width, block_size]) 
avrgd = numpy.mean(t, axis=(1, 3))

最佳答案 要在numpy中逐片计算某些操作,重新整形数组并使用额外的轴通常很有用.

为了解释我们将在这里使用的过程:你可以重塑阵列,取平均值,再次重塑它并再次取平均值.
这里我假设blocksize是2

t = np.array([[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5],[0, 1, 2, 3, 4, 5],[0, 1, 2, 3, 4, 5],[0, 1, 2, 3, 4, 5],[0, 1, 2, 3, 4, 5],])
t = t.reshape([6, 3, 2])
t = np.mean(t, axis=2)
t = t.reshape([3, 2, 3])
np.mean(t, axis=1)

输出

array([[ 0.5,  2.5,  4.5],
       [ 0.5,  2.5,  4.5],
       [ 0.5,  2.5,  4.5]])

现在很清楚它是如何工作的,你只能在一遍中做到:

t = t.reshape([3, 2, 3, 2])
np.mean(t, axis=(1, 3))

也工作(并且应该更快,因为手段只计算一次 – 我猜).我会让你相应地替换height / blocksize,width / blocksize和blocksize.

请参阅@askewcan关于如何将其概括为任何维度的好评.

点赞