sklearn:时间序列数据的用户定义交叉验证 [英] sklearn: User defined cross validation for time series data
问题描述
我正在尝试解决机器学习问题。我有一个带有 time-series 元素的特定数据集。对于这个问题,我使用了著名的python库- sklearn
。这个库中有很多交叉验证迭代器。还有一些迭代器,您可以自己定义交叉验证。问题是我真的不知道如何为时间序列定义简单的交叉验证。这是我想要得到的一个很好的例子:
I'm trying to solve a machine learning problem. I have a specific dataset with time-series element. For this problem I'm using well-known python library - sklearn
. There are a lot of cross validation iterators in this library. Also there are several iterators for defining cross validation yourself. The problem is that I don't really know how to define simple cross validation for time series. Here is a good example of what I'm trying to get:
假设我们有几个时期(年),我们想将数据集分成几个块,如下所示:
Suppose we have several periods (years) and we want to split our data set into several chunks as follows:
data = [1, 2, 3, 4, 5, 6, 7]
train: [1] test: [2] (or test: [2, 3, 4, 5, 6, 7])
train: [1, 2] test: [3] (or test: [3, 4, 5, 6, 7])
train: [1, 2, 3] test: [4] (or test: [4, 5, 6, 7])
...
train: [1, 2, 3, 4, 5, 6] test: [7]
<我真的不明白如何使用sklearn工具创建这种交叉验证。也许我应该使用 sklearn.cross_validation
中的 PredefinedSplit
,像这样:
train_fraction = 0.8
train_size = int(train_fraction * X_train.shape[0])
validation_size = X_train.shape[0] - train_size
cv_split = cross_validation.PredefinedSplit(test_fold=[-1] * train_size + [1] * validation_size)
结果:
train: [1, 2, 3, 4, 5] test: [6, 7]
但是仍然不如以前的数据拆分
But still it's not so good as a previous data split
推荐答案
无需使用 sklearn
,即可获得所需的交叉验证拆分。这是一个示例
You can obtain the desired cross-validation splits without using sklearn
. Here's an example
import numpy as np
from sklearn.svm import SVR
from sklearn.feature_selection import RFECV
# Generate some data.
N = 10
X_train = np.random.randn(N, 3)
y_train = np.random.randn(N)
# Define the splits.
idxs = np.arange(N)
cv_splits = [(idxs[:i], idxs[i:]) for i in range(1, N)]
# Create the RFE object and compute a cross-validated score.
svr = SVR(kernel="linear")
rfecv = RFECV(estimator=svr, step=1, cv=cv_splits)
rfecv.fit(X_train, y_train)
这篇关于sklearn:时间序列数据的用户定义交叉验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!