SlidingWindow

class gtda.time_series.SlidingWindow(size=10, stride=1)[source]

Sliding windows onto the data.

Useful in time series analysis to convert a sequence of objects (scalar or array-like) into a sequence of windows on the original sequence. Each window stacks together consecutive objects, and consecutive windows are separated by a constant stride.

Parameters
  • size (int, optional, default: 10) – Size of each sliding window.

  • stride (int, optional, default: 1) – Stride between consecutive windows.

Examples

>>> import numpy as np
>>> from gtda.time_series import SlidingWindow
>>> # Create a time series of two-dimensional vectors, and a corresponding
>>> # time series of scalars
>>> X = np.arange(20).reshape(-1, 2)
>>> y = np.arange(10)
>>> windows = SlidingWindow(size=3, stride=3)
>>> # Fit and transform X
>>> X_windows = windows.fit_transform(X)
>>> print(X_windows)
[[[ 2  3]
  [ 4  5]
  [ 6  7]]
 [[ 8  9]
  [10 11]
  [12 13]]
 [[14 15]
  [16 17]
  [18 19]]]
>>> # Resample y
>>> yr = windows.resample(y)
>>> print(yr)
[3 6 9]

Notes

The current implementation favours the last entry over the first one, in the sense that the last entry of the last window always equals the last entry in the original time series. Hence, a number of initial entries (depending on the remainder of the division between n_samples - size and stride) may be lost.

__init__(size=10, stride=1)[source]

Initialize self. See help(type(self)) for accurate signature.

fit(X, y=None)[source]

Do nothing and return the estimator unchanged.

This method is here to implement the usual scikit-learn API and hence work in pipelines.

Parameters
  • X (ndarray of shape (n_samples, ..)) – Input data.

  • y (None) – Ignored.

Returns

Return type

self

fit_transform(X, y=None, **fit_params)

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Parameters
  • X (ndarray of shape (n_samples, ..)) – Input data.

  • y (None) – Ignored.

Returns

Xt – Windows of consecutive entries of the original time series. n_windows = (n_samples - size) // stride  + 1.

Return type

ndarray of shape (n_windows, size, ..)

fit_transform_resample(X, y, **fit_params)

Fit to data, then transform the input and resample the target. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X ans a resampled version of y.

Parameters
  • X (ndarray of shape (n_samples, ..)) – Input data.

  • y (ndarray of shape (n_samples,)) – Target data.

Returns

  • Xt (ndarray of shape (n_samples, …)) – Transformed input.

  • yr (ndarray of shape (n_samples, …)) – Resampled target.

get_params(deep=True)

Get parameters for this estimator.

Parameters

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns

params – Parameter names mapped to their values.

Return type

mapping of string to any

resample(y, X=None)[source]

Resample y so that, for any i > 0, the minus i-th entry of the resampled vector corresponds in time to the last entry of the minus i-th window produced by transform.

Parameters
  • y (ndarray of shape (n_samples,)) – Target.

  • X (None) – There is no need for input data, yet the pipeline API requires this parameter.

Returns

yr – The resampled target. n_samples_new = (n_samples - size) // stride + 1.

Return type

ndarray of shape (n_samples_new,)

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters

**params (dict) – Estimator parameters.

Returns

self – Estimator instance.

Return type

object

transform(X, y=None)[source]

Slide windows over X.

Parameters
  • X (ndarray of shape (n_samples, ..)) – Input data.

  • y (None) – Ignored.

Returns

Xt – Windows of consecutive entries of the original time series. n_windows = (n_samples - size) // stride  + 1.

Return type

ndarray of shape (n_windows, size, ..)

transform_resample(X, y)

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Parameters
  • X (ndarray of shape (n_samples, ..)) – Input data.

  • y (ndarray of shape (n_samples,)) – Target data.

Returns

  • Xt (ndarray of shape (n_samples, …)) – Transformed input.

  • yr (ndarray of shape (n_samples, …)) – Resampled target.