1 Upvote

Time Series Cross Validation with Pandas

Python
Data Preparation for Models

In this code snippet we create 5 training and test folds that are saved to csv.

from sklearn.model_selection import TimeSeriesSplit

tscv = TimeSeriesSplit(n_splits=5)
counter = 1

for train_index, test_index in tscv.split(raw_data):
    train, test = raw_data.iloc[train_index], raw_data.iloc[test_index]
    train.to_csv('../data/processed/train_fold_' + str(counter) + '.csv',index=False)
    test.to_csv('../data/processed/test_fold_' + str(counter) + '.csv',index=False)
    counter += 1

By analyseup - Last Updated March 17, 2022, 7:27 p.m.

Did you find this snippet useful?

Sign up to bookmark this in your snippet library

COMMENTS
RELATED SNIPPETS
Scale Data Using Standard Scaler in Sklearn
Python
Data Preparation for Models

Sklearn

3
Create Dummy Variables with Pandas
Python
Data Preparation for Models

Pandas

1
Top Contributors
103
100