如何在 sklearn 中实现前向测试?

how to implement walk forward testing in sklearn?(如何在 sklearn 中实现前向测试?)
本文介绍了如何在 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.

我的想法是:

  1. 编写我自己的k-fold版本类并将其传递给GridSearchCV,这样我就可以享受管道的便利.问题是让 GridSearchCV 使用指定的训练和测试数据索引似乎很困难.

  1. 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.

这篇关于如何在 sklearn 中实现前向测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Seasonal Decomposition of Time Series by Loess with Python(Loess 用 Python 对时间序列进行季节性分解)
Resample a time series with the index of another time series(使用另一个时间序列的索引重新采样一个时间序列)
How can I simply calculate the rolling/moving variance of a time series in python?(如何在 python 中简单地计算时间序列的滚动/移动方差?)
How to use Dynamic Time warping with kNN in python(如何在python中使用动态时间扭曲和kNN)
Keras LSTM: a time-series multi-step multi-features forecasting - poor results(Keras LSTM:时间序列多步多特征预测 - 结果不佳)
Python pandas time series interpolation and regularization(Python pandas 时间序列插值和正则化)