Python中重叠窗口的平均值 [英] Average on overlapping windows in Python
问题描述
我正在尝试计算移动平均线,但是在每个平均线之间设置了步长.例如,如果我正在计算每2个元素4个元素窗口的平均值:
I'm trying to compute a moving average but with a set step size between each average. For example, if I was computing the average of a 4 element window every 2 elements:
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
这应该产生[1、2、3、4],[3、4、5、6],[5、6、7、8],[7、8、9、10]的平均值.
This should produce the average of [1, 2, 3, 4], [3, 4, 5, 6], [5, 6, 7, 8], [7, 8, 9, 10].
window_avg = [2.5, 4.5, 6.5, 8.5]
我的数据使得结尾将在处理之前被截断,因此长度相对于窗口大小没有问题.
My data is such that the ending will be truncated before processing so there is no problem with the length with respect to window size.
我已经阅读了一些有关如何在Python中进行移动平均的知识,并且itertools的用法似乎很多;但是,迭代器一次只包含一个元素,我无法弄清楚在每次平均值计算之间如何具有步长. (如何在Python 3中计算移动平均值? )
I've read a bit about how to do moving averages in Python and there seems to be a lot of usage of itertools; however, the iterators go one element at a time and I can't figure out how to have a step size between each calculation of the average. (How to calculate moving average in Python 3?)
在MATLAB之前,我还能够通过创建重叠的索引矩阵,然后为数据向量建立索引并执行按列的均值(
I have also been able to do this before in MATLAB by creating a matrix of indices which are overlapping and then indexing the data vector and performing a column wise mean (Create matrix by repeatedly overlapping a vector). However, since this vector is rather large (~70 000 elements, window of 450 samples, average every 30 samples), the computation would probably require too much memory.
任何帮助将不胜感激.我正在使用Python 2.7.
Any help would be greatly appreciated. I am using Python 2.7.
推荐答案
在Python中计算列表中滑动窗口平均值的一种方法是使用列表理解.您可以使用
One way to compute the average of a sliding window across a list in Python is to use a list comprehension. You can use
>>> range(0, len(data), 2)
[0, 2, 4, 6, 8]
获取每个窗口的起始索引,然后numpy
的mean
函数获取每个窗口的平均值.请参见下面的演示:
to get the starting indices of each window, and then numpy
's mean
function to take the average of each window. See the demo below:
>>> import numpy as np
>>> window_size = 4
>>> stride = 2
>>> window_avg = [ np.mean(data[i:i+window_size]) for i in range(0, len(data), stride)
if i+window_size <= len(data) ]
>>> window_avg
[2.5, 4.5, 6.5, 8.5]
请注意,列表理解确实具有确保仅计算全窗口"或具有完全window_size
个元素的子列表的平均值的条件.
Note that the list comprehension does have a condition to ensure that it only computes the average of "full windows", or sublists with exactly window_size
elements.
在OP中讨论的大小的数据集上运行时,此方法将在我的MBA上花费200毫秒以上的时间:
When run on a dataset of the size discussed in the OP, this method computes on my MBA in a little over 200 ms:
In [5]: window_size = 450
In [6]: data = range(70000)
In [7]: stride = 30
In [8]: timeit [ np.mean(data[i:i+window_size]) for i in range(0, len(data), stride)
if i+window_size <= len(data) ]
1 loops, best of 3: 220 ms per loop
在我的机器上,它的速度是@Abhijit提出的itertools
方法的两倍:
It is about twice as fast on my machine to the itertools
approach presented by @Abhijit:
In [9]: timeit map(np.mean, izip(*(islice(it, i, None, stride) for i, it in enumerate(tee(data, window_size)))))
1 loops, best of 3: 436 ms per loop
这篇关于Python中重叠窗口的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!