Categories
Python Answers

How to compute the moving average or running mean with Python NumPy?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *