sliding_windows¶
- sliding_windows(X: ndarray, window_size: int, stride: int = 1, axis: int = 1) tuple[ndarray, int] [source]¶
Create sliding windows of a time series.
Extracts sliding windows of a time series with a given window size and stride. The windows are extracted along the specified axis of the time series. If the time series is multivariate, the channels of each window are stacked along the remaining axis.
stride == 1
creates default sliding windows, where the window is moved one time point at a time. Ifstride > 1
, the windows will skip over some time points. Ifstride == window_size
, the windows are non-overlapping, also called tumbling windows.If the time series length minus the window size is not divisible by the stride, the last window will not include the last time points of the time series. The number of time points that are not covered by the windows is returned as padding length.
For a time series with 10 time points, a window size of 3, and a stride of 2, the operation will create the following four windows:
0 7 6 4 4 8 0 6 2 0 0 7 6
- 6 4 4
- 4 8 0
0 6 2
The last point at index 9 (0) is not covered by any window. Thus, the padding length is 1.
- Parameters:
- Xnp.ndarray
Univariate (1d) or multivariate (2d) time series with the shape (n_timepoints, n_channels).
- window_sizeint
Size of the sliding windows.
- strideint, optional, default=1
Stride of the sliding windows. If stride is greater than 1, the windows will skip over some time points.
- axisint, optional, default=1
Axis along which the sliding windows are extracted. The axis must be the time axis of the time series. If
axis == 1
,X
is assumed to have the shape (n_channels, n_timepoints). Ifaxis == 0
,X
is assumed to have the shape (n_timepoints, n_channels).
- Returns:
- windowsnp.ndarray
Sliding windows of the time series. The return shape depends on the
axis
parameter. Ifaxis == 0
, the windows have the shape(n_windows, window_size)
(univariate) or(n_windows, window_size * n_channels)
(multivariate). Ifaxis == 1
, the windows have the shape(window_size, n_windows)
(univariate) or(window_size * n_channels, n_windows)
(multivariate).- padding_lengthint
Number of time points that are not covered by the windows. The padding length is always 0 for stride 1.
See also
aeon.utils.windowing.reverse_windowing
Reverse windowing of a time series.
Examples
>>> import numpy as np >>> from aeon.utils.windowing import sliding_windows >>> X = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) >>> windows, _ = sliding_windows(X, window_size=4) >>> print(windows) [[ 1 2 3 4] [ 2 3 4 5] [ 3 4 5 6] [ 4 5 6 7] [ 5 6 7 8] [ 6 7 8 9] [ 7 8 9 10]] >>> windows, padding = sliding_windows(X, window_size=4, stride=4) >>> print(windows) [[1 2 3 4] [5 6 7 8]] >>> print(padding) 2