Python 对 numpy 数组的每 n 个元素求平均值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15956309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:27:17  来源:igfitidea点击:

Averaging over every n elements of a numpy array

pythonnumpyaverage

提问by user1654183

I have a numpy array. I want to create a new array which is the average over every consecutive triplet of elements. So the new array will be a third of the size as the original.

我有一个 numpy 数组。我想创建一个新数组,它是每个连续三元组元素的平均值。所以新数组的大小将是原始数组的三分之一。

As an example:

举个例子:

 np.array([1,2,3,1,2,3,1,2,3])

should return the array:

应该返回数组:

 np.array([2,2,2])

Can anyone suggest an efficient way of doing this? I'm drawing blanks.

谁能提出一种有效的方法来做到这一点?我在画空白。

采纳答案by Jaime

If your array arrhas a length divisible by 3:

如果您的数组arr的长度可被 3 整除:

np.mean(arr.reshape(-1, 3), axis=1)

Reshaping to a higher dimensional array and then performing some form of reduce operation on one of the additional dimensions is a staple of numpy programming.

重构为更高维的数组,然后在其中一个附加维度上执行某种形式的归约操作是 numpy 编程的主要内容。

回答by L_W

For googlers looking for a simple generalisation for arrays with multiple dimensions: the function block_reducein the scikit-imagemodule (link to docs).

让Google寻找一个简单概括为具有多个尺寸的阵列:该函数block_reducescikit-image模块(链接到文档)。

It has a very simple interface to downsample arrays by applying a function such as numpy.mean, but can also use others (maximum, median, ...). The downsampling can be done by different factors for different axes by supplying a tuple with different sizes for the blocks. Here's an example with a 2D array; downsampling only axis 1 by 10 using the mean:

它有一个非常简单的接口,可以通过应用诸如 之类的函数来对数组进行下采样numpy.mean,但也可以使用其他函数(最大值、中值等)。通过为块提供具有不同大小的元组,可以通过不同轴的不同因素来完成下采样。这是一个二维数组的例子;使用平均值仅对轴 1 进行 10 次下采样:

import numpy as np
from skimage.measure import block_reduce

arr = np.stack((np.arange(1,20), np.arange(20,39)))

# array([[ 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, 36, 37, 38]])

arr_reduced = block_reduce(arr, block_size=(1,5), func=np.mean, cval=np.mean(arr))

# array([[ 3. ,  8. , 13. , 17.8],
#        [22. , 27. , 32. , 33. ]])

As it was discussed in the comments to the other answer: if the array in the reduced dimension is not divisible by block size, padding values are provided by the argument cval(0 by default).

正如在另一个答案的评论中所讨论的那样:如果减少维度中的数组不能被块大小整除,则填充值由参数提供cval(默认为 0)。