Skip to content

Instantly share code, notes, and snippets.

@TuringMachinegun
Created May 22, 2018 20:10
Show Gist options
  • Save TuringMachinegun/9372eff5d590ce9824fb8955b99a6bf0 to your computer and use it in GitHub Desktop.
Save TuringMachinegun/9372eff5d590ce9824fb8955b99a6bf0 to your computer and use it in GitHub Desktop.
from __future__ import division
import numpy as np
def sliding_window(arr, window_size, step=0):
"""Assuming a time series with time advancing along dimension 0,
window the time series with given size and step.
:param arr : input array.
:type arr: numpy.ndarray
:param window_size: size of sliding window.
:type window_size: int
:param step: step size of sliding window. If 0, step size is set to obtain
non-overlapping contiguous windows (that is, step=window_size).
Defaults to 0.
:type step: int
:return: array
:rtype: numpy.ndarray
"""
n_obs = arr.shape[0]
# validate arguments
if window_size > n_obs:
raise ValueError(
"Window size must be less than or equal "
"the size of array in first dimension."
)
if step < 0:
raise ValueError("Step must be positive.")
n_windows = 1 + int(np.floor( (n_obs - window_size) / step))
obs_stride = arr.strides[0]
windowed_row_stride = obs_stride * step
new_shape = (n_windows, window_size) + arr.shape[1:]
new_strides = (windowed_row_stride, ) + arr.strides
strided = np.lib.stride_tricks.as_strided(
arr,
shape=new_shape,
strides=new_strides,
)
return strided
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment