Skip to content

TS Windowing

ML-TS: stationarity is defined three times (Cheatsheet "Basics", Cheatsheet #1, §3), forecasting methods are listed twice (Cheatsheet #5, §6), and windowing appears twice (Ebook 04, Géron 04).


Windowing turns a time series into a supervised-learning dataset: each sample pairs an input window of past time steps with a label window of future time steps.

Parameters

  • Input width: number of time steps fed to the model.

  • Offset (a.k.a. shift): number of steps from the end of the input window to the end of the label window.

  • Total width = input width + offset.

  • Label width: number of future time steps to predict. The labels are the last label width steps of the total window.

  • Features: which columns are inputs, which are labels, or both. The target's own past values (its lags) are usually inputs too.

Example Input Offset Label Total
Predict the next hour from the last 24 24 1 1 25
Predict 24 h ahead from the last 24 24 24 1 48
Predict next week (7 days) from the last week 7 7 7 14

Sliding windows

Slide the window one step at a time, so the windows overlap and the data yields many more samples. A series of length N (stride 1) gives

\[\text{windows} = N - \text{total width} + 1\]

Example: 159 weeks × 7 = 1,113 days. With a 7-day input and a 7-day label: 1113 − 14 + 1 = 1,100 samples, vs 159 with non-overlapping weeks.

Shape for RNN / LSTM / Conv1D

Keras expects 3D input: [samples, time steps, features], where samples is the batch dimension.

  • One week of daily univariate data: [1, 7, 1]

  • The 1,100 windows above: [1100, 7, 1]

  • Tabular data is 2D (rows × columns); time series adds the time-steps axis.

Gotchas

  • Split train / val / test by time before windowing, so no window straddles a boundary (leakage).

  • If multivariate data is stored by standard week ([159, 7, 8]), flatten it to [1113, 8] first, then window across the continuous series.