Expanding-window walk-forward time series cross-validator, scikit-learn compatible.
Project description
walkforwardsplit
An expanding-window, walk-forward time series cross-validator, compatible with
scikit-learn's CV interface (split, get_n_splits) and built on top of
sklearn.model_selection.BaseCrossValidator.
Unlike sklearn.model_selection.TimeSeriesSplit, this splitter always starts
training from a fixed initial block (the first half of the data) and walks
forward through equal-sized test folds carved out of the second half — a
common setup for walk-forward validation in quantitative finance and other
sequential-data settings.
Fold 1: #########################=====....................
Fold 2: ##############################=====...............
Fold 3: ###################################=====..........
Fold 4: ########################################=====.....
Fold 5: #############################################=====
Legend: # train = test . not yet used
Training expands forward each fold while the test block moves ahead in lockstep, always immediately after the training data. Rows further in the future than the current test block ("not yet used") are excluded from both — a fold should never see data from beyond its own test window, even in training.
Install
pip install walkforwardsplit
Usage
import numpy as np
from walkforwardsplit import WalkForwardSplit
X = np.arange(100)
cv = WalkForwardSplit(5)
for train_idx, test_idx in cv.split(X):
print(train_idx, test_idx)
[ 0 1 2 ... 47 48 49] [50 51 52 53 54 55 56 57 58 59]
[ 0 1 2 ... 57 58 59] [60 61 62 63 64 65 66 67 68 69]
[ 0 1 2 ... 67 68 69] [70 71 72 73 74 75 76 77 78 79]
[ 0 1 2 ... 77 78 79] [80 81 82 83 84 85 86 87 88 89]
[ 0 1 2 ... 87 88 89] [90 91 92 93 94 95 96 97 98 99]
With a scikit-learn dataset
Because it implements split() and get_n_splits(), it drops into any
sklearn API that accepts a cv object — here it's used with
cross_val_score on a simple synthetic regression dataset:
import numpy as np
from sklearn.datasets import make_regression
from sklearn.linear_model import Ridge
from sklearn.model_selection import cross_val_score
from walkforwardsplit import WalkForwardSplit
X, y = make_regression(n_samples=200, n_features=5, noise=10, random_state=42)
cv = WalkForwardSplit(n_folds=5)
model = Ridge()
scores = cross_val_score(model, X, y, cv=cv, scoring="r2")
print("R2 per fold:", scores)
print("Mean R2:", scores.mean())
R2 per fold: [0.984 0.989 0.987 0.987 0.976]
Mean R2: 0.985
How the split works
- The first
len(X) // 2rows form the initial training block. - The remaining rows are divided into
n_foldsequal-sized test blocks (the last fold absorbs any remainder). - On each iteration, the training set expands to include everything up to the start of the current test block; the test block never overlaps with training.
- Rows beyond the current test block are excluded from both train and test for that fold — they belong to later folds.
If n_folds is too large for the dataset (each test block would be empty),
split() raises a ValueError rather than silently yielding empty folds.
Migrating from CustomTimeSeriesSplit
Earlier versions exposed this class as CustomTimeSeriesSplit. It's still
importable as a deprecated alias of WalkForwardSplit with identical
behavior, but emits a DeprecationWarning and will be removed in a future
release:
# old (still works, but warns)
from walkforwardsplit import CustomTimeSeriesSplit
# new
from walkforwardsplit import WalkForwardSplit
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file walkforwardsplit-0.2.0.tar.gz.
File metadata
- Download URL: walkforwardsplit-0.2.0.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
39007f9406d3ccebec1a142ede716a6509140416c84e4888009133fe17a55761
|
|
| MD5 |
cbb34ec1d28e55539cd2cdbfa6434207
|
|
| BLAKE2b-256 |
fd905db9bba1d6714d452ed772399e9e96fbfd40640d4263e42ccbe324b3a35c
|
File details
Details for the file walkforwardsplit-0.2.0-py3-none-any.whl.
File metadata
- Download URL: walkforwardsplit-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f070803eb0227667ef35f48fedbe9db44205735b1fdcfe3603b05f89f068b32f
|
|
| MD5 |
5e861bd8712a3f182997b9124d30cd94
|
|
| BLAKE2b-256 |
056d1d66bca86b9265c7d4f7169a2441bfe779edf6f836877947dc6960c6cd42
|