Skip to content

Instantly share code, notes, and snippets.

@zakhar
Created November 9, 2015 16:14
Show Gist options
  • Select an option

  • Save zakhar/26a1997155dd12a57d9a to your computer and use it in GitHub Desktop.

Select an option

Save zakhar/26a1997155dd12a57d9a to your computer and use it in GitHub Desktop.
numpy rolling window
# from http://stackoverflow.com/a/6811241
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment