Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rolling window for 1D arrays in Numpy?

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.

like image 346
c00kiemonster Avatar asked Jul 25 '11 02:07

c00kiemonster


People also ask

Which is the correct way to create NumPy 1D array?

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.

What is 1D array in NumPy?

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.

How do I transpose one direction array in NumPy?

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).

What is a rolling window?

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.


2 Answers

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) 
like image 175
so12311 Avatar answered Sep 20 '22 08:09

so12311


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]]) 
like image 39
Xavier Guihot Avatar answered Sep 21 '22 08:09

Xavier Guihot