# SlidingWindow¶

class gtda.time_series.SlidingWindow(width=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
• width (int, optional, default: 10) – Width of each sliding window. Each window contains width + 1 objects from the original time series.

• 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(width=2, 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_\mathrm{ samples} - \mathrm{width} - 1$$ and the stride) may be lost.

__init__(width=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 - width - 1) // stride  + 1, and n_samples_window = width + 1.

Return type

ndarray of shape (n_windows, n_samples_window, ..)

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

static plot(Xt, sample=0)[source]

Plot a sample from a collection of sliding windows, as a point cloud in 2D or 3D. If points in the window have more than three dimensions, only the first three are plotted.

Important: when using on the result Xt of calling transform on X, ensure that each sample in X is a point in n_dimensions-dimensional space with n_dimensions > 1.

Parameters
• Xt (ndarray of shape (n_samples, n_points, n_dimensions)) – Collection of sliding windows, each containing n_points points in n_dimensions-dimensional space, such as returned by transform.

• sample (int, optional, default: 0) – Index of the sample in Xt to be plotted.

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 - time_delay * (dimension - 1) - 1) // 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 - width - 1) // stride  + 1, and n_samples_window = width + 1.

Return type

ndarray of shape (n_windows, n_samples_window, ..)

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.