Sobes.tech
Middle — Senior

How to perform cross-validation on time series?

sobes.tech AI

Answer from AI

Cross-validation on time series differs from the classical one because the data have temporal dependence, and they cannot be simply shuffled randomly.

Main approaches:

  1. Time Series Split (sliding window)

    • Data are divided into consecutive time blocks.
    • At each step, the model is trained on data up to a certain point in time and tested on the next time interval.
    • Example: train on months 1-3, test on 4; then train on 1-4, test on 5, and so on.
  2. Walk-forward validation

    • Similar to Time Series Split, but the model is retrained at each step, adding new data.
  3. Blocked cross-validation

    • Data are divided into blocks, avoiding information leakage between training and testing.

Example in Python using sklearn:

from sklearn.model_selection import TimeSeriesSplit
import numpy as np

X = np.arange(100).reshape(-1, 1)  # Example data
y = np.sin(X).ravel()

tscv = TimeSeriesSplit(n_splits=5)
for train_index, test_index in tscv.split(X):
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]
    # Train model on X_train, y_train
    # Evaluate on X_test, y_test

It is important to maintain the chronological order to avoid using future data for training the model.