# giotto.time_series.SlidingWindow¶

class giotto.time_series.SlidingWindow(width=10, stride=1)

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
widthint, optional, default: 10

Width of each sliding window. Each window contains width + 1 objects from the original time series.

strideint, optional, default: 1

Stride between consecutive windows.

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.

Examples

>>> import numpy as np
>>> from giotto.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]


Methods

 fit(self, X[, y]) Do nothing and return the estimator unchanged. fit_transform(self, X[, y]) Fit to data, then transform it. fit_transform_resample(self, X, y, …) Fit to data, then transform the input and resample the target. get_params(self[, deep]) Get parameters for this estimator. resample(self, y[, X]) 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. set_params(self, \*\*params) Set the parameters of this estimator. transform(self, X[, y]) Slide windows over X. transform_resample(self, X, y) Fit to data, then transform it.
__init__(self, width=10, stride=1)

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

fit(self, X, y=None)

Do nothing and return the estimator unchanged.

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

Parameters
Xndarray, shape (n_samples, …)

Input data.

yNone

Ignored.

Returns
self
fit_transform(self, 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
Xndarray of shape (n_samples, …)

Input data.

yNone

There is no need for a target in a transformer, yet the pipeline API requires this parameter.

Returns
Xtnumpy array of shape (n_samples, …)

Transformed input.

fit_transform_resample(self, 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
Xndarray of shape (n_samples, …)

Input data.

yndarray of shape (n_samples, )

Target data.

Returns
Xtndarray of shape (n_samples, …)

Transformed input.

yrndarray of shape (n_samples, …)

Resampled target.

get_params(self, deep=True)

Get parameters for this estimator.

Parameters
deepboolean, optional

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns
paramsmapping of string to any

Parameter names mapped to their values.

resample(self, y, X=None)

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
yndarray, shape (n_samples,)

Target.

XNone

There is no need for input data, yet the pipeline API requires this parameter.

Returns
yrndarray, shape (n_samples_new,)

The resampled target. n_samples_new = (n_samples - time_delay * (dimension - 1) - 1) // stride + 1.

set_params(self, **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.

Returns
self
transform(self, X, y=None)

Slide windows over X.

Parameters
Xndarray, shape (n_samples, …)

Input data.

yNone

Ignored.

Returns
Xtndarray, shape (n_windows, n_samples_window, …)

Windows of consecutive entries of the original time series. n_windows = (n_samples - width - 1) // stride  + 1, and n_samples_window = width + 1.

transform_resample(self, 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
Xndarray of shape (n_samples, …)

Input data.

yndarray of shape (n_samples, )

Target data.

Returns
Xtndarray of shape (n_samples, …)

Transformed input.