我正在尝试预测股票价格,但出现此错误。
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为形状
和
test_data.head()
就是这样以638为形状
我希望这有帮助。我仍然不确定为什么这不起作用。
在您提供的
forecast()
代码行中,您要求的变量 (fc, se, and conf)
与 forecast()
函数实际返回的变量之间似乎不匹配。 forecast()
模块中的statsmodels.tsa.arima.model.ARIMAResults
函数仅返回指定时间段内的预测值,而不返回se
和conf
变量。
我认为您对
ARMA
模型和 ARIMA
模型感到困惑。在 ARMA
模型中,forecast()
方法返回这 3 个值。
要解决此处的问题,您可以修改代码以仅询问预测值。这是更新后的代码:
# Only ask for the forecasted values
fc = fitted.forecast(638, alpha=0.05)
通过进行此更改,您将收到指定时间段(在给定示例中为 638 天)的未来值 (fc)
Series
。
您可以参考
ARIMA
文档这里。
并且,您可以参考
ARMA
文档这里
这是一个工作示例:
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