API Reference / evaluation / splitting /

timeseries.py

Time series cross-validation splitters.

Classes

TimeSeriesSplit

class evaluation.splitting.timeseries.TimeSeriesSplit(BaseSplitter)

Time Series cross-validation splitter.

Provides train/test indices for time series data where test set is always in the future relative to training set.
Constructor
__init__(
    self,
    n_splits: int = 5,
    test_size: Optional[int] = None,
    gap: int = 0,
    max_train_size: Optional[int] = None,
)

Parameters

n_splits
int = 5
Number of splits.
test_size
int
Size of test set. If None, uses n_samples // (n_splits + 1).
gap
int = 0
Number of samples to skip between train and test.
max_train_size
int
Maximum size for a single training set.

Raises

ValueError
From split when n_splits * test_size + gap leaves no room for a training set, since that configuration cannot yield the promised number of folds.

Notes

Unlike regular K-Fold, training set grows with each split:
  • Split 1: train=[0:n], test=[n:n+test_size]
  • Split 2: train=[0:n+test_size], test=[n+test_size:n+2*test_size]
  • etc.
python
>>> from tuiml.evaluation.splitting import TimeSeriesSplit
>>> import numpy as np
>>> X = np.arange(10).reshape(-1, 1)
>>> tss = TimeSeriesSplit(n_splits=3)
>>> for train_idx, test_idx in tss.split(X):
...     print(f"Train: {train_idx}, Test: {test_idx}")
Train: [0 1 2 3], Test: [4 5]
Train: [0 1 2 3 4 5], Test: [6 7]
Train: [0 1 2 3 4 5 6 7], Test: [8 9]

Methods

get_parameter_schema (cls) -> dict

Return JSON Schema for parameters.

split (self, X: np.ndarray, y: Optional[np.ndarray]=None, groups: Optional[np.ndarray]=None) -> Iterator[Tuple[np.ndarray, np.ndarray]]

Generate time series split indices.

get_n_splits (self, X: Optional[np.ndarray]=None, y: Optional[np.ndarray]=None, groups: Optional[np.ndarray]=None) -> int

Get the number of splits this splitter yields.

Parameters
X
np.ndarray
Ignored, present for API consistency.
y
np.ndarray
Ignored, present for API consistency.
groups
np.ndarray
Ignored, present for API consistency.
Returns
n_splits
int
Number of train/test pairs split yields. Configurations that could not deliver this count raise in split rather than silently yielding fewer folds.
__repr__ (self) -> str

Return a reproducible string form of the splitter.

Returns
repr_str
str
Constructor-style representation, e.g. TimeSeriesSplit(n_splits=5, test_size=None, gap=0).