Is there a SciPy function or NumPy function or module for Python that calculates the running mean of a 1D array given a specific window?
All the aforementioned solutions are poor because they lack
numpy.cumsum
, orO(len(x) * w)
implementations as convolutions.Given
import numpy
m = 10000
x = numpy.random.rand(m)
w = 1000
Note that x_[:w].sum()
equals x[:w-1].sum()
. So for the first average the numpy.cumsum(...)
adds x[w] / w
(via x_[w+1] / w
), and subtracts 0
(from x_[0] / w
). This results in x[0:w].mean()
Via cumsum, you'll update the second average by additionally add x[w+1] / w
and subtracting x[0] / w
, resulting in x[1:w+1].mean()
.
This goes on until x[-w:].mean()
is reached.
x_ = numpy.insert(x, 0, 0)
sliding_average = x_[:w].sum() / w + numpy.cumsum(x_[w:] - x_[:-w]) / w
This solution is vectorized, O(m)
, readable and numerically stable.