SARIMAX的样本不足预测问题

时间:2018-12-14 10:59:23

标签: python forecasting arima forecast

我可以对样本数据进行预测,但是当我尝试从样本预测中做出预测时,我会收到一条错误消息:

C:\Users\YannickLECROART\Miniconda3\envs\machinelearning\lib\site-packages\statsmodels\tsa\base\tsa_model.py:531: ValueWarning: No supported index is available. Prediction results will be given with an integer index beginning at `start`.
  ValueWarning)
<statsmodels.tsa.statespace.mlemodel.PredictionResultsWrapper object at 0x000001F303476A58>

通过单击下面的链接,您可以找到我使用的数据集。

https://ufile.io/an2cx

import warnings
import itertools
import numpy as np
import matplotlib.pyplot as plt
warnings.filterwarnings("ignore")
plt.style.use('fivethirtyeight')
import pandas as pd
import statsmodels.api as sm
import matplotlib

matplotlib.rcParams['axes.labelsize'] = 14
matplotlib.rcParams['xtick.labelsize'] = 12
matplotlib.rcParams['ytick.labelsize'] = 12
matplotlib.rcParams['text.color'] = 'k'

首先,我从Excel文件中提取数据集。

df = pd.read_excel("C:\\Users\\YannickLECROART\\Desktop\\comedie.xlsx", index_col=0)

然后,我将数据帧转换为时间序列。

df.index = pd.to_datetime(df.index)

我对数据进行排序,这样我早上只能得到9到10之间的值。

idx_9 = df.between_time('09:00', '09:59')

我配置了SARIMAX参数

mod = sm.tsa.statespace.SARIMAX(idx_0,
                                order=(1, 1, 1),
                                seasonal_order=(1, 1, 0, 12),
                                enforce_stationarity=False,
                                enforce_invertibility=False)

results = mod.fit()

然后我对样本数据进行预测,以将其与观测值进行比较

pred = results.get_prediction(start=1, dynamic=False)
pred_ci = pred.conf_int()

ax = idx_9['2017':].plot(label='Observations')
pred.predicted_mean.plot(ax=ax, label='Prédictions', alpha=.7, figsize=(14, 7))

ax.fill_between(pred_ci.index,
                pred_ci.iloc[:, 0],
                pred_ci.iloc[:, 1], color='k', alpha=.2)

ax.set_xlabel('Date')
ax.set_ylabel('Places occupées')
plt.legend()

plt.show()

这是情节的样子

enter image description here

最后,我想根据样本预测进行绘制,以便在观察后将其绘制出来,这是我收到错误消息的地方:

pred_uc = results.get_forecast(steps=100)
pred_ci = pred_uc.conf_int()

ax = idx_0.plot(label='Observations', figsize=(14, 7))
pred_uc.predicted_mean.plot(ax=ax, label='Prédictions')
ax.fill_between(pred_ci.index,
                pred_ci.iloc[:, 0],
                pred_ci.iloc[:, 1], color='k', alpha=.25)
ax.set_xlabel('Date')
ax.set_ylabel('Places occupées')
plt.legend()
plt.show()

您能告诉我为什么会收到此错误消息以及如何解决该错误消息吗?提前致谢。

1 个答案:

答案 0 :(得分:2)

要使用日期进行预测,您的索引必须为DatetimeIndexPeriodIndex,并具有相关的频率,例如月,日,分钟等。

就您而言,我想您每天都有几分钟的数据,我认为这与熊猫的频率不符。因此,它会执行预测,只是不知道如何为预测分配新日期。

如果您知道如何为预测期构建日期索引,则可以这样做,并将其作为index参数传递。例如

fcast_index = pd.to_datetime(['2017-04-02 9:00am', '2017-04-02 9:00am', ...])
pred_uc = results.get_forecast(steps=100, index=fcast_index)