Explain Codes LogoExplain Codes Logo

How to calculate rolling / moving average using python + NumPy / SciPy?

python
numpy
scipy
dataframe
Nikita BarsukovbyNikita Barsukov·Feb 15, 2025
TLDR

To calculate a rolling average in NumPy, employ the numpy.convolve function:

import numpy as np data = np.array([your_data]) window_size = your_window_size window = np.ones(window_size) / window_size rolling_avg = np.convolve(data, window, 'valid')

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.

def moving_average_cumsum(data, window_size): cumsum_vec = np.cumsum(np.insert(data, 0, 0)) return (cumsum_vec[window_size:] - cumsum_vec[:-window_size]) / window_size

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!

from scipy.signal import convolve def moving_average_fft(data, window_size): window = np.ones(window_size) / window_size return convolve(data, window, mode='same', method='fft')

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:

import pandas as pd series = pd.Series(data) rolling_avg = series.rolling(window=window_size).mean()

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:

rolling_avg_ew = series.ewm(span=window_size).mean()

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.

import bottleneck as bn rolling_avg_bn = bn.move_mean(data, window=window_size, min_count=1) from numpy.lib.stride_tricks import sliding_window_view def moving_average_sliding_window(data, window_size): return np.mean(sliding_window_view(data, window_shape=window_size), axis=-1)

Multidimensional smoothing

Say you're working with 2D arrays, NumPy and SciPy can help you out:

# 2D convolution for image data smoothing from scipy.signal import convolve2d image_data = np.random.rand(5,5) # Example 2D array kernel = np.ones((3, 3)) / 9 # Defining a 3x3 smoothing kernel smoothed_image = convolve2d(image_data, kernel, boundary='fill', fillvalue=0, mode='same')