python-3.x - scikit learn 机器学习中时间序列的交叉验证

标签 python-3.x machine-learning scikit-learn linear-regression cross-validation

我无法找到我正在寻找的信息,所以我将在这里发布我的问题。 我刚刚涉足机器学习领域。我使用 scikit learn 库对时间序列进行了第一次多元回归。我的代码如下所示

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()

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

最佳答案

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

为了做出 future 的预测,您应该使用整个数据和最佳超参数重新拟合您的回归器,或者,正如 @Marcus V. 在评论中提到的,也许最好只使用最新的数据来训练它。

关于python-3.x - scikit learn 机器学习中时间序列的交叉验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48939388/

相关文章:

Python - 分割非json请求的响应

python-3.x - 对大型数据框中的选定列进行重新排序

machine-learning - torch7 中图像保存和加载的异常行为

python - Sklearn 0.20+ 的交叉验证?

django - user.has_perm 是 false,即使我可以在管理页面和 user.user_permissions 中看到它

machine-learning - 在 Keras 中使用卷积神经网络后使用 LSTM 时出现尺寸误差

python - 如何知道我的标签在 python 中的混淆矩阵中正确排列?

python - sklearn Standardscaler() 可以影响测试矩阵结果

scikit-learn - 将 scikit-learn SVM 模型转换为 LibSVM

python - 按第三个、第二个、第一个元素对列表列表进行排序