time-series - Python时间序列拆分
问题描述
我有以下问题:
我有一个计时器系列。我已经完成了预处理,现在我有了包含多个特征的 x 和包含我的输出的 y。我已经把它分成了火车,测试:x_train
,,,,x_test
y_train
y_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)
解决方案
根据 sklearn 文档:
https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.TimeSeriesSplit.html
“提供训练/测试索引来拆分在训练/测试集中以固定时间间隔观察到的时间序列数据样本。在每次拆分中,测试索引必须高于以前,因此在交叉验证器中改组是不合适的。”
如果你想验证一个时间序列模型,那么要走的路是嵌套交叉验证,一些关于它的信息在下面的链接中:
https://mlfromscratch.com/nested-cross-validation-python-code/
推荐阅读
- wcf - WCF 服务在本地主机上运行(调试)但在部署时失败
- reactjs - 按 Tab 键关闭从子菜单打开的 Material UI 对话框
- python - 创建具有给定文件名但文件名已被占用的文件的最佳实践
- json - 当我通过 AJAX 获取 JSON 数据时,数据存储在哪里?
- r - 使用混合效应模型交叉验证多个时间序列数据集
- php - 使用 imaplib 时的 Bash 与 CMD
- java - 为什么递归调用打印'123456'而不是'1'?
- javascript - 为什么人们使用复杂的函数来检查 cookie?
- sql - 带有 IN 子句的 Excel 参数化查询
- git - git 没有检测到更改的文件