【问题标题】:Python Time Series ARIMA ForecastPython 时间序列 ARIMA 预测
【发布时间】:2017-05-01 00:12:14
【问题描述】:

我正在尝试实施时间序列预测,并按照此处找到的有用教程进行操作: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() 中添加日期参数:

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

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

提前致谢。

【问题讨论】:

    标签: python time-series forecasting


    【解决方案1】:

    您的索引似乎没有正确定义。

    所以问题出在原来的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()
    

    Resulting Plot

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-06
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多