numpy 2D数组的块均值 [英] Block mean of numpy 2D array

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

问题描述

我想在NumPy中找到2D数组的块均值.为简单起见,让我们假设数组如下:

I want to find block mean of a 2D array in NumPy. For simplicity, let us assume that the array is as follows:

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]])

我想将此数组划分为3个大小为2x4的块,然后找到所有三个块的均值(这样均值的形状为2x4.第一个块是由前4列组成的,下一个是接下来的4列,依此类推.所以我的代码块是:

I want to divide this array into 3 blocks of size 2x4, and then find the mean of all three blocks (so that the shape of the mean is 2x4. The first block is formed by the first 4 columns, the next one by the next 4 columns and so on. So my blocks are:

array([[0, 1, 2, 3],
       [12, 13, 14, 15]])

array([[ 4,  5,  6,  7],
       [16, 17, 18, 19]])

array([[ 8,  9, 10, 11],
       [20, 21, 22, 23]])

我可以使用循环来执行此操作,但是我觉得最好先通过reshape将此数组转换为3D数组,然后沿第三轴在3D数组上使用mean方法.这可能类似于此问题.

I can use a loop to do this but I get a feel that it would be better to first convert this array into a 3D array by reshape and then use the mean method on the 3D array along the third axis. This could be similar to this question.

如果有人可以为我提供帮助,将不胜感激

Would appreciate if someone can provide me with:

1).如果存在这样的窍门,那么适当的Pythonic命令来执行块均值甚至无需转换为3D即可.

1). An appropriate Pythonic command to do the block mean without even converting to 3D, if such a trick exists.

2).如果不是合适的Pythonic命令来进行2D到3D转换.

2). If not an appropriate Pythonic command to do the 2D to 3D conversion.

3).深入了解使用循环或使用上面的命令来执行此操作是否更有效(就空间而言).

3). An insight whether it would be more efficient (in terms of space) to do it using a loop or by using the command above.

推荐答案

numpy方法几乎总是会击败python循环,所以我将跳过您的1..

Numpy methods are going to beat python loops almost always, so I am going to skip your 1.

对于2,在这种特殊情况下,可以完成以下工作:

As for 2, in this particular case the following works:

a = np.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]])
a = a.reshape(2, 3, 4)
>>> a
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]]])
>>> np.mean(a, axis=1)
array([[  4.,   5.,   6.,   7.],
       [ 16.,  17.,  18.,  19.]])

诀窍在reshape中.对于通常需要n列的块的一般情况,以下是一个选项

The trick is in the reshape. For a general case where you want blocks of n columns, the following is an option

a = a.reshape((a.shape[0], -1, n))

您对3个问题的担忧大多没有根据. reshape 返回原始数组的视图,不是副本,因此转换为3D只需更改数组的shapestrides属性,而不必复制任何实际数据.

Your concerns in 3 are mostly unwarranted. reshape returns a view of the original array, not a copy, so the conversion to 3D only requires altering the shape and strides attributes of the array, without having to copy any of the actual data.

编辑 为确保重塑不会复制数组,而是返回视图,请按照以下方式进行重塑

EDIT To be sure that reshaping does not copy the array, but returns a view, do the reshape as

a.shape = a = a.reshape((a.shape[0], -1, n))

docs 中的示例的行:

>>> a = np.arange(12).reshape(3,4)
>>> b = a.T
>>> b.shape = (12,)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: incompatible shape for a non-contiguous array

通常,只有对阵列进行了transposerollaxisswapaxes等操作时才有问题.

And in general there are only problems if you have been doing transpose, rollaxis, swapaxes or the like on your array.

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

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