cross_validation 用于 scikit learn 机器学习中的时间序列

cross_validation for time series in scikit learn machine learning

我找不到我要找的信息,所以我会 post 我的问题在这里。 我只是冒险进入机器学习。我使用 scikit 学习库对时间序列进行了第一次多元回归。我的代码如下图

X = df[feature_cols]
y = df[['scheduled_amount']]
index= y.reset_index().drop('scheduled_amount', axis=1)
linreg = LinearRegression()
tscv = TimeSeriesSplit(max_train_size=None, n_splits=11)
li=[]
for train_index, test_index in tscv.split(X):
    train = index.iloc[train_index]
    train_start, train_end = train.iloc[0,0], train.iloc[-1,0]
    test = index.iloc[test_index]
    test_start, test_end = test.iloc[0,0], test.iloc[-1,0]
    X_train, X_test = X[train_start:train_end], X[test_start:test_end]
    y_train, y_test = y[train_start:train_end], y[test_start:test_end]
    linreg.fit(X_train, y_train)
    y_predict = linreg.predict(X_test)
    print('RSS:' + str(linreg.score(X_test, y_test)))
    y_test['predictec_amount'] = y_predict
    y_test.plot()

并不是说我的数据是时间序列数据,我想在拟合模型时将日期时间索引保留在我的 Dataframe 中。 我正在使用 TimeSeriesSplit 进行交叉验证。我仍然不太了解交叉验证的事情。 首先是需要在时间序列数据集中进行交叉验证。其次,我应该使用最后一个 linear_coeff_ 还是应该获取所有这些的平均值以用于我未来的预测。

是的,时间序列数据集中需要 cross-validation。基本上,您需要确保您的模型不会过度拟合您当前的测试,并且能够捕获过去的季节性变化,这样您就可以对模型在未来做同样的事情有信心。此方法还用于选择模型超参数(即 Ridge 回归中的 alpha)。

为了做出未来的预测,您应该使用全部数据和最佳超参数重新调整回归器,或者正如@Marcus V. 在评论中提到的那样,也许最好只使用最新数据对其进行训练。