如何在 sklearn 中实现前向测试?
问题描述
在 sklearn 中,GridSearchCV 可以将管道作为参数,通过交叉验证找到最佳估计器.然而,通常的交叉验证是这样的:
In sklearn, GridSearchCV can take a pipeline as a parameter to find the best estimator through cross validation. However, the usual cross validation is like this:
为了交叉验证时间序列数据,训练和测试数据通常是这样拆分的:
to cross validate a time series data, the training and testing data are often splitted like this:
也就是说,测试数据应该总是领先于训练数据.
That is to say, the testing data should be always ahead of training data.
我的想法是:
编写我自己的k-fold版本类并将其传递给GridSearchCV,这样我就可以享受管道的便利.问题是让 GridSearchCV 使用指定的训练和测试数据索引似乎很困难.
Write my own version class of k-fold and passing it to GridSearchCV so I can enjoy the convenience of pipeline. The problem is that it seems difficult to let GridSearchCV to use an specified indices of training and testing data.
写一个新的类GridSearchWalkForwardTest,类似于GridSearchCV,正在研究grid_search.py源码,发现有点复杂.
Write a new class GridSearchWalkForwardTest which is similar to GridSearchCV, I am studying the source code grid_search.py and find it is a little complicated.
欢迎提出任何建议.
解决方案
我认为你可以使用 时间序列拆分 代替您自己的实现或作为实现与您描述的完全相同的 CV 方法的基础.
I think you could use a Time Series Split either instead of your own implementation or as a basis for implementing a CV method which is exactly as you describe it.
经过一番挖掘,似乎有人在 这个 PR 看起来就像你想要的一样.
After digging around a bit, it seems like someone added a max_train_size to the TimeSeriesSplit in this PR which seems like it does what you want.
相关文章