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 leak.

train gap test

Installation

This repository is not registered, but you can clone it into your own project and use it with ease.

git clone https://github.com/WenjieZ/TSCV.git tscv
mkdir YOURPROJECT/tscv
cp tscv/split.py YOURPROJECT/tscv/split.py
cp tscv/__init__.py YOURPROJECT/tscv/__init__

YOURPROJECT is the name of your project folder.

Usage

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

  • GapLeaevPOut
  • 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)

Support

See the documentation here.

If you need any further help, please use the issue tracker.

Authors

This extension is mainly developed by me, Wenjie Zheng.

The GapWalkForward cross-validator is adapted from the TimeSeriesSplit of scikit-learn.

Acknowledgment

  • I would like to thank Christoph Bergmeir, Prabir Burman, and Jeffrey Racine for the helpful discussion.
  • I would like to thank Jacques Joubert for encouraging me to develop this package.

License

BSD-3-Clause

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.1.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

tscv-0.0.1-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tscv-0.0.1.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.5.6

File hashes

Hashes for tscv-0.0.1.tar.gz
Algorithm Hash digest
SHA256 4a156e4337c3bcd445529992231ddb77798e6d51d6beb74cd28ed9b6814805d0
MD5 6747447d67f0a5d5cd227b40aa9d0455
BLAKE2b-256 3d7077c6442775640997e192256c82a0251186be549145d0999df0af6381a364

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tscv-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.5.6

File hashes

Hashes for tscv-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 703f1f414f49364b037185aea23531a296e4054e52b85b45766da3c371d44caa
MD5 6fa3084e5fdf2847238c8769ce835255
BLAKE2b-256 b525c417d12946c27aa2d42dd6db823495831a02c26282111eea13966456ed21

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