Numpy重新绑定2D数组 [英] Numpy rebinning a 2D array

查看:78
本文介绍了Numpy重新绑定2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种快速的公式来对2D numpy数组进行数值合并.通过分箱,我的意思是计算子矩阵平均值或累积值.对于前. x = numpy.arange(16).reshape(4,4)会被分成4个子矩阵,每个2x2,并给出numpy.array([[2.5,4.5],[10.5,12.5]])其中2.5 = numpy.平均值([0,1,4,5])等...

I am looking for a fast formulation to do a numerical binning of a 2D numpy array. By binning I mean calculate submatrix averages or cumulative values. For ex. x = numpy.arange(16).reshape(4, 4) would have been splitted in 4 submatrix of 2x2 each and gives numpy.array([[2.5,4.5],[10.5,12.5]]) where 2.5=numpy.average([0,1,4,5]) etc...

如何以有效的方式执行此类操作...我真的不知道如何执行此操作...

How to perform such an operation in an efficient way... I don't have really any ideay how to perform this ...

非常感谢...

推荐答案

您可以使用数组的更高维度视图,并沿额外维度取平均值:

You can use a higher dimensional view of your array and take the average along the extra dimensions:

In [12]: a = np.arange(36).reshape(6, 6)

In [13]: a
Out[13]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35]])

In [14]: a_view = a.reshape(3, 2, 3, 2)

In [15]: a_view.mean(axis=3).mean(axis=1)
Out[15]: 
array([[  3.5,   5.5,   7.5],
       [ 15.5,  17.5,  19.5],
       [ 27.5,  29.5,  31.5]])

通常,如果要为(rows, cols)的数组分配形状为(a, b)的容器,则将其重塑为.reshape(rows // a, a, cols // b, b).还请注意.mean的顺序很重要,例如a_view.mean(axis=1).mean(axis=3)会引发错误,因为a_view.mean(axis=1)仅具有三个维度,尽管a_view.mean(axis=1).mean(axis=2)可以正常工作,但是却很难理解发生了什么.

In general, if you want bins of shape (a, b) for an array of (rows, cols), your reshaping of it should be .reshape(rows // a, a, cols // b, b). Note also that the order of the .mean is important, e.g. a_view.mean(axis=1).mean(axis=3) will raise an error, because a_view.mean(axis=1) only has three dimensions, although a_view.mean(axis=1).mean(axis=2) will work fine, but it makes it harder to understand what is going on.

照原样,上面的代码仅在您可以在数组中容纳整数个bin的情况下才有效,即a除以rows并且b除以cols.有很多方法可以处理其他情况,但是您将必须定义自己想要的行为.

As is, the above code only works if you can fit an integer number of bins inside your array, i.e. if a divides rows and b divides cols. There are ways to deal with other cases, but you will have to define the behavior you want then.

这篇关于Numpy重新绑定2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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