Chronological model evaluation
Forecasting or other temporal prediction tasks
Scikit-Learn · Class
TimeSeriesSplit is a cross-validator for time-ordered data. It builds progressively later test folds while training on earlier observations, with optional limits on training size, test size, and a gap.
TimeSeriesSplit creates successive chronological train/test partitions for ordered observations. Each later training partition includes earlier data, while gap can remove samples immediately before testing; equally spaced samples are needed when fold metrics must represent equal time durations.
class sklearn.model_selection.TimeSeriesSplit(n_splits=5, *, max_train_size=None, test_size=None, gap=0)| Name | Type | Description |
|---|---|---|
n_splits | int, default=5 | Number of splits; must be at least 2. |
max_train_size | int, default=None | Maximum size for a single training set. |
test_size | int, default=None | Limits the size of the test set; defaults to n_samples // (n_splits + 1). |
gap | int, default=0 | Number of samples excluded from the end of each training set before testing. |
A cross-validator that yields train/test index arrays for successive time-ordered splits.
import numpy as np
from sklearn.model_selection import TimeSeriesSplit
X = np.arange(12).reshape(6, 2)
cv = TimeSeriesSplit(n_splits=3, gap=1, test_size=1)
for train, test in cv.split(X):
print(train, test)Creates chronological folds while leaving one sample between each training and test segment.
Forecasting or other temporal prediction tasks
Pipelines with a delay between observed training data and evaluated outcomes
Non-stationary systems where old data should be discarded
TimeSeriesSplit is designed for ordered observations. It produces training indices from earlier folds and a test fold that occurs later, avoiding evaluation on the past after training on the future.
Follow-up: How would you prevent leakage from a forecast horizon? Set gap to exclude the required number of samples between the training end and test start.
The default n_splits is 5, and it must be at least 2. In split k, the first k folds form training data and the next fold forms test data.
Follow-up: What happens as n_splits increases? Each default test partition becomes smaller because its size is based on n_samples divided by n_splits plus one.
max_train_size limits the size of an individual training set. test_size limits the test-set size; when omitted, it defaults to n_samples // (n_splits + 1).
Follow-up: When would max_train_size be useful? Use it when only a recent rolling window should train the model rather than the entire accumulated history.
KFoldcross-validationTimeSeriesSplit.split