Time Series Cross Validation with Pandas

Python

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

 1|  from sklearn.model_selection import TimeSeriesSplit
 2|  
 3|  tscv = TimeSeriesSplit(n_splits=5)
 4|  counter = 1
 5|  
 6|  for train_index, test_index in tscv.split(raw_data):
 7|      train, test = raw_data.iloc[train_index], raw_data.iloc[test_index]
 8|      train.to_csv('../data/processed/train_fold_' + str(counter) + '.csv',index=False)
 9|      test.to_csv('../data/processed/test_fold_' + str(counter) + '.csv',index=False)
10|      counter += 1
Did you find this snippet useful?

Sign up for free to to add this to your code library