How to calculate rolling / moving average using python + NumPy / SciPy?
To calculate a rolling average in NumPy, employ the numpy.convolve
function:
Replace your_data
and your_window_size
with your dataset and desired window length. This method provides equal weight to all data points in the window, excluding edge cases. If your data is a mainstream chart-topper, this is an efficient way to average it out.
Simplicity meets performance
When it comes to calculating rolling averages, there are more approaches than Marvel has superheroes. Let's explore.
Cumulative sum
The np.cumsum
yields a non-weighted moving average as fast as lightning whilst remaining as simple as a peanut butter sandwich.
Isn't that just sum-thing else?
FFT meets SciPy
Some problems need Hulk-like power to solve. With very large datasets, you might benefit from using an FFT-based method. Time to summon SciPy!
Can you hear the data crunch?
Time-series and Pandas
If time-series data is your villain, Pandas gallops to the rescue with a user-friendly rolling
method:
This shows that Pandas do more than just eat bamboo!
Advanced options
Exponential weighting
Recent data giving you a headache? Aim an exponentially-weighted moving window at it:
Going for performance
When speed matters, bottleneck.move_mean
might just deliver the performance boost you need, while numpy.lib.stride_tricks.sliding_window_view
offers a fresh and modern way to tame the rolling window.
Multidimensional smoothing
Say you're working with 2D arrays, NumPy and SciPy can help you out:
Was this article helpful?