繁体   English   中英

Python时间序列ARIMA预测

[英]Python Time Series ARIMA Forecast

我正在尝试实施时间序列预测并遵循此处的有用教程: https : //www.analyticsvidhya.com/blog/2016/02/time-series-forecasting-codes-python/

在对数转换等之后,我的时间序列对象的样本可以在下面找到:

ts = 

2015-02-01  4.532599
2015-03-01  7.635787
2015-04-01  7.698029
2015-05-01  4.564348
2015-06-01  4.744932
2015-09-01  5.365976
2015-10-01  7.657283
2016-02-01  7.059618
2016-03-01  5.433722
2016-04-01  7.600902

当我试图尝试建立AR模型时,即

model = ARIMA(ts_log, order=(1, 1, 0))
results_AR = model.fit(disp=-1)
plt.plot(ts_log)
plt.plot(results_AR.fittedvalues, color='red')
plt.title('RSS: %.4f'% sum((results_AR.fittedvalues-ts_log_diff)**2))
plt.show()

我的问题是当我尝试这样做时,我得到:

TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U21') dtype('<U21') dtype('<U21')

当我在时间序列索引中打印出数据类型时,我得到:

DatetimeIndex(['2015-05-01'], dtype='datetime64[ns]', freq=None)

然后,我尝试像这样在ARIMA()中添加date参数:

model = ARIMA(dt_ts, order=(1, 1, 0), dates=dt_ts.index.values)

或通过添加一个单独的数组来格式化我所有的日期:

model = ARIMA(dt_ts, order=(1, 1, 0), dates=formatted)

在两种情况下,我都得到了这一点:

ValueError: Given a pandas object and the index does not contain dates

任何人都知道为什么会发生这种情况以及如何解决它?

提前致谢。

看来您的索引定义不正确。

因此,问题出在最初的ts。

我尝试使用下面的代码复制错误,并且运行正常。

import pandas as pd
from statsmodels.tsa.arima_model import ARIMA

periods = 10000
my_index = pd.date_range('2016-07-01', periods=periods, freq='D')
data = np.random.randint(100,1000,periods)
ts = pd.Series(data=data, index=my_index, name='Monthly Returns')
ts_log=np.log(ts).diff().dropna()
print(ts.index)

DatetimeIndex(['2016-07-01', '2016-07-02', '2016-07-03', '2016-07-04',
           '2016-07-05', '2016-07-06', '2016-07-07', '2016-07-08',
           '2016-07-09', '2016-07-10',
           ...
           '2043-11-07', '2043-11-08', '2043-11-09', '2043-11-10',
           '2043-11-11', '2043-11-12', '2043-11-13', '2043-11-14',
           '2043-11-15', '2043-11-16'],
          dtype='datetime64[ns]', length=10000, freq='D')

而且,如果我们进行剩余的计算,则它的绘制应该没有任何错误。

model = ARIMA(ts_log, order=(1, 1, 0))
results_AR = model.fit(disp=-1)
plt.plot(ts_log)
plt.plot(results_AR.fittedvalues, color='red')
plt.title('RSS: %.4f'% sum((results_AR.fittedvalues-ts_log)**2))
plt.show()

结果图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM