Time-Series Cross-Validation Splits

easy
Unit 6 · Kernels0/3 to milestone

Shuffled k-fold on time series trains on the future to predict the past. Time-series splits always train on earlier data and test on the block right after it.

Implement time_series_splits(n, n_splits, test_size=None, gap=0, max_train_size=None), matching scikit-learn's TimeSeriesSplit:

  • test_size defaults to n // (n_splits + 1).
  • Split i (0-based) tests on [start, start + test_size) with start = n - (n_splits - i) * test_size.
  • Training uses indices [0, start - gap), keeping only the last max_train_size indices if that's set (a sliding window).
  • Return a list of (train, test) pairs of Python lists. Raise ValueError if any training set would be empty.

Then implement check_no_leakage(splits): True if in every split all training indices are smaller than all test indices.

#cross-validation#time-series#leakage2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Time-Series Cross-Validation Splits · TechGradify