Is there a way to get a Sliding Nested Cross Validation using SKlearn?

Viewed 1663

I'm currently working with some time series data and I'm using TimeSeriesSplit in order to split my data set into a forward chaining cross validation splits.

So if i have 100 data points - And I divide into 3 splits. 1. I train on 1-25. Test on 26-50. 2. Train on 1-50. Test on 51-75. 3. Train on 1-75. Test on 76-100.

Call this an expanding window example.

I'm wondering if there is a way to slide the training window forward every time i train so it's not starting from 0. I'm trying to achieve the sliding window scenario similar to the diagram

enter image description here

1 Answers

Unfortunately, there isn't a sliding window CV available in sklearn specifically for time series cross validation. However, using StratifiedKFold or KFold with the parameter shuffle=False can mimic non-randomization. Note that this also holds for train_test_split, which is useful for time series data as well.

Here is the sklearn documentation page for visualizing the various cross-validation behaviors:

https://scikit-learn.org/stable/auto_examples/model_selection/plot_cv_indices.html#sphx-glr-auto-examples-model-selection-plot-cv-indices-py

Another alternative is to split by indices using the Python pandas or collections libraries. Pandas has good support for time series-related concepts as well, such as rolling windows.

Related