我无法找到我正在寻找的信息,所以我将在这里发布我的问题。 我刚刚涉足机器学习领域。我使用 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/