首页 > 解决方案 > ARIMA 预测验证

问题描述

我在 ARIMA 模型上运行验证,当代码执行时,绘图不正确。我试图更改参数但没有运气,导致 x 轴的数据无法正确绘制。

数据集样本:

Date    consume forecast
2017-01-01  122 141
2017-02-01  122 141
2017-03-01  122 141
2017-04-01  122 141
2017-05-01  122 141
2017-06-01  122 141
2017-07-01  122 141
2017-08-01  122 141
2017-09-01  10  340
2017-10-01  36  46
2017-11-01  268 141
2017-12-01  176 37
2018-01-01  2293 746
2018-02-01  958 12
2018-03-01  305 12
2018-04-01  179 80
2018-05-01  259 12
2018-06-01  173 5486
2018-07-01  821 276
2018-08-01  438 746
2018-09-01  825 746
2018-10-01  768 84
2018-11-01  2191 746
2018-12-01  897 12
2019-01-01  1147 3688
2019-02-01  393 4905
2019-03-01  736 1294
2019-04-01  1269 1015
2019-05-01  1594 419

代码:

p = d = q = range(0, 2)
pdq = list(itertools.product(p, d, q))
seasonal_pdq = [(x[0], x[1], x[2], 12) for x in list(itertools.product(p, d, q))]

print('Examples of parameter combinations for Seasonal ARIMA...')
print('SARIMAX: {} x {}'.format(pdq[1], seasonal_pdq[1]))
print('SARIMAX: {} x {}'.format(pdq[1], seasonal_pdq[2]))
print('SARIMAX: {} x {}'.format(pdq[2], seasonal_pdq[3]))
print('SARIMAX: {} x {}'.format(pdq[2], seasonal_pdq[4]))

for param in pdq:
    for param_seasonal in seasonal_pdq:
        try:
            mod = sm.tsa.statespace.SARIMAX(y,order=param,seasonal_order=param_seasonal,enforce_stationarity=False,enforce_invertibility=False)


            results = mod.fit()

            print('ARIMA{}x{}12 - AIC:{}'.format(param, param_seasonal, results.aic))
        except:
            continue

mod = sm.tsa.statespace.SARIMAX(y,
                                order=(1, 1, 1),
                                seasonal_order=(1, 1, 1, 12),
                                enforce_invertibility=False)
results = mod.fit()
print(results.summary().tables[1])

results.plot_diagnostics(figsize=(15, 12))
plt.show()

pred = results.get_prediction(start=pd.to_datetime('2020-01'), dynamic=False)
pred_ci = pred.conf_int()
ax = y['2017-01':].plot(label='Actual')
pred.predicted_mean.plot(ax=ax, label='Predicted - Test', 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('Forecasted vCPU')
plt.legend()
plt.show()

预期与此类似:

在此处输入图像描述

我得到的是:

在此处输入图像描述

标签: pythonarima

解决方案


通过在运行 ARIMA 代码之前重新格式化日期列来纠正问题:

数据['日期']=pd.to_datetime(数据['日期'])


推荐阅读