3

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

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()

这就是情节的样子

在此处输入图像描述

最后,我想做出样本预测,以便在观察后绘制它,这就是我收到错误消息的地方:

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()

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

4

1 回答 1

4

要使用日期执行预测,您的索引必须是 aDatetimeIndexPeriodIndex,并具有相关的频率,如月、日、分钟等。

就您而言,我猜您每天都有几分钟的数据,我认为这与 Pandas 频率不对应。出于这个原因,它确实执行了预测,只是不知道如何为预测分配新的日期。

如果您知道如何构建预测期的日期索引,那么您可以这样做并将其作为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)
于 2018-12-27T01:33:46.383 回答