使用NumPy数组计算平均值

4

我有一个 2D 的 numpy 数组(6 行 6 列),我想要创建另一个 2D 数组,其中每个块是在一个块大小窗口内所有元素的平均值。目前,我有以下代码:

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)

有没有办法让它更符合Python语言特性?或者说numpy已经有类似的功能了吗?

更新

根据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))

你能提供一组输入和期望的输出吗?你的代码不是很容易理解啊 ^^ - daniel451
修改代码以包含示例输入和预期输出。 - user308827
1个回答

2

在numpy中逐个操作计算非常有用,通常需要重新调整数组并使用额外的轴。

为了解释这里的过程:您可以重新调整数组,取平均值,再次重新调整它并再次取平均值。 这里假设块大小为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/blocksizewidth/blocksizeblocksize

参见 @askewcan 的好建议,以便将其推广到任何维度。


1
要扩展到超出二维(并且让“块大小”在每个轴上都不同),请使用a.reshape(shape).mean(axes),其中 shape = itertools.chain(*np.broadcast(a.shape/blocksize, blocksize))axes = tuple(range(1, 2*a.ndim, 2)) - askewchan
谢谢!@askewchan,我遇到了以下错误:TypeError:不支持的操作数类型(除法):“tuple”和“int”,出错行为:shape = itertools.chain(*numpy.broadcast(data.shape/block_size, block_size)). - user308827
抱歉,这假定 blocksize 是一个numpy数组。 - askewchan
嗨,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)) - user308827
@user308827,这段代码对我来说运行良好。出了什么问题吗?你是否得到了意外的值或错误信息?你可以使用//进行整数除法,而不是调用np.floor - P. Camilleri
好的,我只是想确认一下我是否正确地进行了泛化,谢谢! - user308827

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