Sometimes, we want to compute the moving average or running mean with Python NumPy.
In this article, we’ll look at how to compute the moving average or running mean with Python NumPy.
How to compute the moving average or running mean with Python NumPy?
To compute the moving average or running mean with Python NumPy, we can use the SciPy uniform_filter1d
method.
For instance, we write
import numpy as np
from scipy.ndimage.filters import uniform_filter1d
N = 1000
x = np.random.random(100000)
y = uniform_filter1d(x, size=N)
to create a array with
x = np.random.random(100000)
Then we calculate the running mean by calling uniform_filter1d
with array x
size
set to N
.
Conclusion
To compute the moving average or running mean with Python NumPy, we can use the SciPy uniform_filter1d
method.