Skip to main content

Time series cross-validation

Project description

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

Update

pip install tscv --upgrade

Usage

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

  • GapLeavePOut
  • GapKFold
  • GapWalkForward
  • gap_train_test_split

The three classes can all be passed, as the cv argument, to the cross_val_score function in scikit-learn, just like the native cross-validator classes in scikit-learn.

The one function is an alternative to the train_test_split 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.model_selection import cross_val_score
from tscv import GapKFold

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

# use GapKFold as the cross-validator
cv = GapKFold(n_splits=5, gap_before=5, gap_after=5)
scores = cross_val_score(clf, iris.data, iris.target, cv=cv)

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

import numpy as np
from tscv import gap_train_test_split

X, y = np.arange(20).reshape((10, 2)), np.arange(10)
X_train, X_test, y_train, y_test = gap_train_test_split(X, y, test_size=2, gap_size=2)

Contributing

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

Authors

This extension is developed mainly by Wenjie Zheng.

The GapWalkForward cross-validator is adapted from the TimeSeriesSplit of scikit-learn (see Kyle Kosic's PR scikit-learn/scikit-learn#13204).

Support

  • If you want to support this project, please consider being a sponsor.
  • If you use this package in your research, please consider citing it in your paper.

Acknowledgments

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

License

BSD-3-Clause

Citation

@article{zheng2019hv,
  title={$ hv $-Block Cross Validation is not a BIBD: a Note on the Paper by Jeff Racine (2000)},
  author={Zheng, Wenjie},
  journal={arXiv preprint arXiv:1910.08904},
  year={2019}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tscv-0.0.5.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

tscv-0.0.5-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file tscv-0.0.5.tar.gz.

File metadata

  • Download URL: tscv-0.0.5.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for tscv-0.0.5.tar.gz
Algorithm Hash digest
SHA256 f6a59d9e21d6e3cb283d27e1486e4d3a9c8d88667779fb8e2a74cf3db976d814
MD5 0364b282417520f0d5384e95e5b9ec30
BLAKE2b-256 6dafeda65b4ec846e0f9a7906be394e193995824f52a68f9716937993a5fd277

See more details on using hashes here.

File details

Details for the file tscv-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: tscv-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for tscv-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 edb1513cfbd391fce2af13cbb22f0f0c622b33232b78d4b6300ccbc449caf1d6
MD5 c355d105e9a942f524e019683365e4d1
BLAKE2b-256 cdc5f3ae90bfe164eb186fba4b88a06b3e007cc06c6fbc4457e9ce644df8c9fa

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page