Is there a way to efficiently implement a rolling window for 1D arrays in Numpy?
For example, I have this pure Python code snippet to calculate the rolling standard deviations for a 1D list, where observations
is the 1D list of values, and n
is the window length for the standard deviation:
stdev = [] for i, data in enumerate(observations[n-1:]): strip = observations[i:i+n] mean = sum(strip) / n stdev.append(sqrt(250*sum([(s-mean)**2 for s in strip])/(n-1)))
Is there a way to do this completely within Numpy, i.e., without any Python loops? The standard deviation is trivial with numpy.std
, but the rolling window part completely stumps me.
I found this blog post regarding a rolling window in Numpy, but it doesn't seem to be for 1D arrays.
Create 1D Numpy Array using arange() function Numpy arange() function takes start, end of a range and the interval as arguments and returns a one-dimensional array. In this example, we will import numpy library and use arange() function to crate a one dimensional numpy array.
One dimensional array contains elements only in one dimension. In other words, the shape of the NumPy array should contain only one value in the tuple.
If you want to turn your 1D vector into a 2D array and then transpose it, just slice it with np. newaxis (or None , they're the same, newaxis is just more readable).
A Rolling window is expressed relative to the delivery date and automatically shifts forward with the passage of time. For example, a customer with a 5-year Rolling window who gets a delivery on May 4, 2015 would receive data covering the period from May 4, 2015 to May 4, 2020.
Just use the blog code, but apply your function to the result.
i.e.
numpy.std(rolling_window(observations, n), 1)
where you have (from the blog):
def rolling_window(a, window): shape = a.shape[:-1] + (a.shape[-1] - window + 1, window) strides = a.strides + (a.strides[-1],) return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
Starting in Numpy 1.20
, you can directly get a rolling window with sliding_window_view
:
from numpy.lib.stride_tricks import sliding_window_view sliding_window_view(np.array([1, 2, 3, 4, 5, 6]), window_shape = 3) # array([[1, 2, 3], # [2, 3, 4], # [3, 4, 5], # [4, 5, 6]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With