numpy数组的计算平均值 [英] Computing average for numpy array

查看:1141
本文介绍了numpy数组的计算平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a 2d numpy array (6 x 6) elements. I want to create another 2D array out of it, where each block is the average of all elements within a blocksize window. Currently, I have the foll. code:

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已经有东西了?

Is there any way I can make it more pythonic? Perhaps numpy has something which does it already?

更新

基于下面的M. Massias's soln,这是一个用变量替换固定值的更新.不知道它是否编码正确.它似乎确实可以工作:

Based on M. Massias's soln below, here is an update with fixed values replaced by variables. Not sure if it is coded right. it does seem to work though:

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中逐片计算某些操作,通常需要重塑数组并使用额外的轴.

To compute some operation slice by slice in numpy, it is very often useful to reshape your array and use extra axes.

为解释这一过程,我们将在这里使用:您可以重整数组,取均值,再次重整并再次取均值. 在这里,我假设块大小为2

To explain the process we'll use here: you can reshape your array, take the mean, reshape it again and take the mean again. Here I assume blocksize is 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]])

现在很清楚这是如何工作的,您只能通过一遍来完成:

Now that it's clear how this works, you can do it in one pass only:

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

也可以工作(并且应该更快一些,因为均值只能计算一次-我猜).我将让您分别替换height/blocksizewidth/blocksizeblocksize.

works too (and should be quicker since means are computed only once - I guess). I'll let you substitute height/blocksize, width/blocksize and blocksize accordingly.

有关如何将其推广到任何维度的信息,请参见@askewcan.

See @askewcan nice remark on how to generalize this to any dimension.

这篇关于numpy数组的计算平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆