首页 > 解决方案 > Python时间序列拆分

问题描述

我有以下问题:

我有一个计时器系列。我已经完成了预处理,现在我有了包含多个特征的 x 和包含我的输出的 y。我已经把它分成了火车,测试:x_train,,,,x_testy_trainy_test

我现在想做回归和网格搜索。

由于我有时间序列,我无法进行 k 折交叉验证。所以我想使用TimeSeriesSplit.

但我到底在分裂什么?我想我会把训练集分成训练和测试/验证来训练我的模型,验证/选择我的超参数,然后使用测试进行预测。这个对吗?我该如何选择n_splits

我现在有以下代码:

pipe=Pipeline....
pipe.fit(x_train, y_train)

tss=TimeSeriesSplit(n_splits=5)
for train_index, test_index in tss(train):
print('train:', train_index, 'test:', test_index

clf=GridSearchCV(pipe, param_grid, cv=tss)
clf.fit(x_train, y_train)

标签: time-series

解决方案


根据 sklearn 文档:

https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.TimeSeriesSplit.html

“提供训练/测试索引来拆分在训练/测试集中以固定时间间隔观察到的时间序列数据样本。在每次拆分中,测试索引必须高于以前,因此在交叉验证器中改组是不合适的。”

如果你想验证一个时间序列模型,那么要走的路是嵌套交叉验证,一些关于它的信息在下面的链接中:

https://mlfromscratch.com/nested-cross-validation-python-code/


推荐阅读