WenjieZ
TSCV
Python

Time Series Cross-Validation -- an extension for scikit-learn

Last updated May 29, 2026
259
Stars
43
Forks
4
Issues
0
Stars/day
Attention Score
57
Language breakdown
Python 100.0%
Files click to expand
README

Downloads Build Status codecov Documentation Status DOI

TSCV: Time Series Cross-Validation

This repository is a scikit-learn extension for time series cross-validation. It introduces gaps between the training set and the test set, which mitigates the temporal dependence of time series and prevents information leakage.

Installation

pip install tscv

or

conda install -c conda-forge tscv

Usage

This extension defines 3 cross-validator classes and 1 function:

  • GapLeavePOut
  • GapKFold
  • GapRollForward
  • gaptraintest_split
The three classes can all be passed, as the cv argument, to scikit-learn functions such as cross-validate, crossvalscore, and crossvalpredict, just like the native cross-validator classes.

The one function is an alternative to the traintestsplit function in scikit-learn.

Examples

The following example uses GapKFold instead of KFold as the cross-validator.

import numpy as np from sklearn import datasets from sklearn import svm from sklearn.modelselection import crossval_score from tscv import GapKFold

iris = datasets.load_iris() clf = svm.SVC(kernel='linear', C=1)

use GapKFold as the cross-validator

cv = GapKFold(nsplits=5, gapbefore=5, gap_after=5) scores = crossvalscore(clf, iris.data, iris.target, cv=cv)

The following example uses gaptraintest_split to split the data set into the training set and the test set.

import numpy as np from tscv import gaptraintest_split

X, y = np.arange(20).reshape((10, 2)), np.arange(10) Xtrain, Xtest, ytrain, ytest = gaptraintestsplit(X, y, testsize=2, gap_size=2)

Contributing

  • Report bugs in the issue tracker
  • Express your use cases in the issue tracker

Documentations

Acknowledgments

  • I would like to thank Jeffrey Racine and Christoph Bergmeir for the helpful discussion.

License

BSD-3-Clause

Citation

Wenjie Zheng. (2021). Time Series Cross-Validation (TSCV): an extension for scikit-learn. Zenodo. http://doi.org/10.5281/zenodo.4707309

@software{zheng20214707309,
  title={{Time Series Cross-Validation (TSCV): an extension for scikit-learn}},
  author={Zheng, Wenjie},
  month={april},
  year={2021},
  publisher={Zenodo},
  doi={10.5281/zenodo.4707309},
  url={http://doi.org/10.5281/zenodo.4707309}
}
🔗 More in this category

© 2026 GitRepoTrend · WenjieZ/TSCV · Updated daily from GitHub