ARIMA模型循环python
我有一个在 train[:350] test[350:427] 值的数据集中训练的 arima 模型。我正在将模型拟合到训练值中,我的 (p,d,q) 值为 (1,1,2)。目前我一次只能预测一个时间步。我想循环运行模型,以便每次输出一个预测值时,都会将其添加到训练数据集中,并使用新值来预测另一个新的预测值。我无法理解如何做到这一点,到目前为止这就是我所得到的。
historical = train['max']
predictions = []
for t in range(len(test)):
model = ARIMA(historical, order=(1,1,2))
model_fit = model.fit()
output = model_fit.forecast(exog=test['max'][t])
predictions.append(output)
observed = test['max'][t]
historical.append(predictions)
print(len(historical))
I have an arima Model trained in a dataset of train[:350] test[350:427] values. I Am fitting the model in train values and my (p,d,q) values are (1,1,2). Currently i can predict only one time step at a time. I want to run the model in loop so that everytime it outputs one forecasted value, it is added to the train dataset and the new value is used to predict another new forecasted value. I am unable to understand how to do it and so far this is what i have got.
historical = train['max']
predictions = []
for t in range(len(test)):
model = ARIMA(historical, order=(1,1,2))
model_fit = model.fit()
output = model_fit.forecast(exog=test['max'][t])
predictions.append(output)
observed = test['max'][t]
historical.append(predictions)
print(len(historical))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为此,您不需要循环,而是使用预测方法:
model_fit.predict(start, end, endog=test["max"],dynamic=True)
在这种情况下,ARIMA 将添加将预测值添加到您的数据中,以便做出新的预测。如果您想使用实际值更新数据,请设置
dynamic=False
https://www.statsmodels.org/dev/ generated/statsmodels.tsa.arima.model.ARIMAResults.predict.html#statsmodels.tsa.arima.model.ARIMAResults.predict
You do not need a loop for this, you use the predict method instead:
model_fit.predict(start, end, endog=test["max"], dynamic=True)
In this case ARIMA will add the predicted values to your data in order to make new predictions. If you want to update your data with the real values instead, you set
dynamic=False
https://www.statsmodels.org/dev/generated/statsmodels.tsa.arima.model.ARIMAResults.predict.html#statsmodels.tsa.arima.model.ARIMAResults.predict