numpy ARIMA预测在Python中不起作用,错误为“要解压缩的值太多(预期为3)”

eivnm1vs  于 2023-08-05  发布在  Python
关注(0)|答案(1)|浏览(93)

我试图预测股票价格,我得到了这个错误。

import statsmodels.api as smapi
model = smapi.tsa.arima.ARIMA(train_data, order = (1,1,0))
fitted = model.fit()
print(test_data.shape)
fc,se,conf=fitted.forecast(638, alpha = 0.05)
fc_series = pd.Series(fc, index = test_data.index)
lower_series = pd.Series(conf[:, 0], index = test_data.index)
upper_series = pd.Series(conf[:, 1], index = test_data.index)

字符串
对于此行fc,se,conf=fitted.forecast(638, alpha = 0.05)
我得到的错误是

ValueError: too many values to unpack (expected 3)


我不确定是什么导致了错误。我输入了638,因为这是test_data的形状
我真的很感激在这方面的帮助!

  • 编辑 *

train_data.head()是这样的,形状为1888
x1c 0d1x的数据
test_data.head()是这样的,形状为638



希望这能帮上忙。我仍然不确定为什么这不起作用。

lf3rwulv

lf3rwulv1#

在您提供的forecast()代码行中,您为(fc, se, and conf)请求的变量与forecast()函数实际返回的变量之间似乎不匹配。statsmodels.tsa.arima.model.ARIMAResults模块中的forecast()函数仅返回指定时间段的预测值,而不返回seconf变量。
我想你是混淆了ARMA模型和ARIMA模型。在ARMA模型中,forecast()方法返回这3个值。
要解决此处的问题,您可以修改代码以仅请求预测值。下面是更新后的代码:

# Only ask for the forecasted values
fc = fitted.forecast(638, alpha=0.05)

字符串
通过进行此更改,您将收到指定时间段(在给定示例中为638天)的未来值(fc)的Series
您可以参考ARIMA文档here
您可以参考ARMA文档here
这里有一个工作示例:

import numpy as np
import statsmodels.api as smapi

# ensure reproducibility
np.random.seed(42)

# time series for one year with daily frequency
time_index = pd.date_range(start='2013-07-01', periods=365, freq='D')
# random price values for training data
values = np.random.rand(365)*10
# create the dataframe
train_data = pd.DataFrame(values, index=time_index, columns=['Price'])

# time series for one month of test
time_index = pd.date_range(start='2014-07-01', periods=30, freq='D')
# random price values for test data
values = np.random.rand(30)*10
# create the dataframe
test_data = pd.DataFrame(values, index=time_index, columns=['Price'])

model = smapi.tsa.arima.ARIMA(train_data, order = (1,1,0))
fitted = model.fit()

fc=fitted.forecast(30)
test_data['forecasted'] = fc
test_data

相关问题