Python/Pandas - 混淆 ARIMA 预测以获得简单的预测

Python/Pandas - confusion around ARIMA forecasting to get simple predictions

试图思考如何实施 ARIMA 模型来生成(可以说)简单的预测。本质上,我要做的是预测今年年底前的预订量并导出为 csv。看起来像这样:

date           bookings
2017-01-01     438
2017-01-02     167
...
2017-12-31     45
2018-01-01     748
...
2018-11-29     223
2018-11-30     98
...
2018-12-30     73
2018-12-31     100

预测比今天 (28/11/18) 更大的地方。

我尝试做的事情:

这给了我我的数据集,它基本上是两列,2017 年全年的每日数据和预订:

import pandas as pd
import statsmodels.api as sm
# from statsmodels.tsa.arima_model import ARIMA
# from sklearn.metrics import mean_squared_error

import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
import matplotlib
matplotlib.rcParams['axes.labelsize'] = 14
matplotlib.rcParams['xtick.labelsize'] = 12
matplotlib.rcParams['ytick.labelsize'] = 12
matplotlib.rcParams['text.color'] = 'k'

df = pd.read_csv('data.csv',names = ["date","bookings"],index_col=0)
df.index = pd.to_datetime(df.index)

这是 'modelling' 位:

X = df.values
size = int(len(X) * 0.66)
train, test = X[0:size], X[size:len(X)]
history = [x for x in train]
predictions = list()
for t in range(len(test)): 
    model = ARIMA(history, order=(1,1,0))
    model_fit = model.fit(disp=0)
    output = model_fit.forecast()

    yhat = output[0]
    predictions.append(yhat) 

    obs = test[t]
    history.append(obs)

    #   print('predicted=%f, expected=%f' % (yhat, obs))
#error = mean_squared_error(test, predictions)
#print(error)
#print('Test MSE: %.3f' % error)
# plot
plt.figure(num=None, figsize=(15, 8))
plt.plot(test)
plt.plot(predictions, color='red')
plt.show()

正在将结果导出到 csv:

df_forecast = pd.DataFrame(predictions)
df_test = pd.DataFrame(test)
result = pd.merge(df_test, df_forecast, left_index=True, right_index=True)
result.rename(columns = {'0_x': 'Test', '0_y': 'Forecast'}, inplace=True)

我遇到的问题是:

我认为我需要做的事情:

我遇到问题的方法和原因。 任何帮助将不胜感激

以下是一些想法:

  • 了解 train/test 个子集。如果我错了请纠正我,但训练集用于训练模型并生成 'predictions' 数据,然后测试是否用于将预测与测试进行比较?

是的,没错。这个想法与任何机器学习模型一样,数据在 train/test 中拆分,使用训练数据拟合模型,测试用于使用一些误差指标将实际模型预测与真实数据进行比较.但是,当您处理时间序列数据时,必须按照时间顺序执行 train/test 拆分,就像您已经做的那样。

  • 2017年的数据看起来不错,但是我如何在2018年的数据上实现呢?我如何获得 Train/Test 集?我什至需要它吗?

你真的有 2018 年数据的 csv 吗?您需要做的所有拆分 train/test 与您对 2017 年数据所做的相同,即保持到一定大小作为训练,然后留下来测试您的预测 train, test = X[0:size], X[size:len(X)]。但是,如果您想要预测今天以后的日期,为什么不使用所有历史数据作为模型的输入并使用它来进行预测呢?

我认为我需要做的事情

  • 在 2017 年和 2018 年拆分它

你为什么要拆分它?只需将所有数据作为单个时间序列序列提供给 ARIMA 模型,从而附加您的年度数据,并使用最后的 size 个样本作为测试。考虑到样本量越大,估计值就越好。验证模型的性能后,使用它从今天开始进行预测。