Skip to content

Instantly share code, notes, and snippets.

@mFoxRU
Last active August 29, 2015 14:17
Show Gist options
  • Save mFoxRU/b368eb0c24df28a57dd5 to your computer and use it in GitHub Desktop.
Save mFoxRU/b368eb0c24df28a57dd5 to your computer and use it in GitHub Desktop.
Modified version of rolling window function from (1) that allows choosing rolling (increment) step. // (1) http://www.rigtorp.se/2011/01/01/rolling-statistics-numpy.html
import numpy as np
def rolling_window(a, window, step=1):
n = (a.shape[-1] - window) // step + 1
shape = a.shape[:-1] + (n, window)
strides = a.strides[:-1] + (a.strides[-1]*step, a.strides[-1])
return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment