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