【问题标题】:Forecasting sales with a 6 years dataset-python用 6 年的数据集预测销售额-python
【发布时间】:2020-05-27 19:47:11
【问题描述】:

我正在尝试根据 2014 年 1 月 1 日 ==> 2020 年 1 月 1 日的 6 年数据集来预测需求。 首先,我尝试按月重新组合需求,因此我最终得到了一个包含 2 列(月和销售额)和 72 行(12 月 * 6 年)的数据集。 P.s:我正在使用 python。

我的第一个问题是:知道我只有 72 行这一事实,是否足以预测明年(2020 年)。

我的第二个问题是,是否有任何模型可以建议我使用并且可以让我获得很好的准确性?

我尝试过结合季节性 (sarimax) 和 LSTM 使用 arima 模型,但它不起作用,我不确定我是否做得对。

我的第三个问题是:python中是否有任何测试可以告诉您是否存在季节性?

#shrink the dataset
dataa=data[(data['Produit']=='ACP NOR/STD')&(data['Région']=='Europe')]

gb2=dataa.groupby(by=[dataa['Mois'].dt.strftime('%Y, %m')])['Chargé (T)'].sum().reset_index()
gb2.Mois=pd.to_datetime(gb2.Mois)

[#create a time serie][2]
series = pd.Series(gb2['Chargé (T)'].values, index=gb2.Mois)


#decompose the dataset to 3 things: trend, seasonality and noise
from pylab import rcParams
import statsmodels.api as sm
rcParams['figure.figsize'] = 18, 8
decomposition = sm.tsa.seasonal_decompose(series, model='additive')
fig = decomposition.plot()
plt.show()


    #calculate acf and pacf to know in which order to stop

    from statsmodels.graphics.tsaplots import plot_acf
    from statsmodels.graphics.tsaplots import plot_pacf
    from matplotlib import pyplot

    pyplot.figure()
    pyplot.subplot(211)
    plot_acf(series, ax=pyplot.gca())
    pyplot.subplot(212)
    plot_pacf(series, ax=pyplot.gca())
    pyplot.show()

import itertools
p = d = q = range(0, 5)
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]))


    import warnings
    warnings.filterwarnings("ignore")
    for param in pdq:
        for param_seasonal in seasonal_pdq:
            try:
                mod = sm.tsa.statespace.SARIMAX(series,
                                                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(series,
                                order=(0, 1, 2),
                                seasonal_order=(0, 4, 0, 12),
                                enforce_stationarity=False,
                                enforce_invertibility=False)

    results = mod.fit()

    print(results.summary().tables[1])
    results.plot_diagnostics(figsize=(16, 8))
    plt.show()
    #get predictions
    pred = results.get_prediction(start=pd.to_datetime('2019-01-01'), dynamic=False)
    pred_ci = pred.conf_int()

    ax = series['2014':].plot(label='observed')
    pred.predicted_mean.plot(ax=ax, label='One-step ahead Forecast', alpha=.8, 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('Chargé (T)')
    plt.legend()

    plt.show()

预测与现实无关... 我真的很感谢任何人的帮助。

【问题讨论】:

    标签: python pandas machine-learning neural-network forecasting


    【解决方案1】:
    1. 据我所知,我们可以使用这样数量的 数据(这意味着您每个月使用 6 个数据点 拟合模型),但尽量使用尽可能多的数据 - 然后你的 准确性只会提高。
    2. 时间序列中几乎总是存在一些季节性,甚至更多, 还有一个趋势。所以你需要分解你原来的时间 序列到趋势、季节和残差,所有预测都将是 用残差完成。关于模型 - ARIMA 就足够了 预测时间序列,使其更精确,只需调整您的 使用 PACF 和 ACF 的参数(p 和 q)。
    3. 我们进行分解以使我们的时间序列平稳,换句话说
      • 从中提取残差(我们应该只在固定数据上训练我们的模型)。您宁愿检查平稳性,而不是季节性
      • 有 ADF 测试。

    我对此进行了大量研究,并有一个关于 ts 预测的项目,here 是示例,其中描述了所有步骤:

    【讨论】:

    • 感谢您的快速回答,非常感谢!你检查了我的 arima 代码吗?我看不出我在模型中做错了什么......我将订单推到了 5(PACF 和 ACF 但仍然不够好)
    • 我可以看到,您首先尝试找出最佳参数,然后(我认为您使用 SARIMAX 模型的 (0, 1, 2) 参数,因为它们是最好的)采用最佳参数并进行拟合和训练模型。 * 尝试使用 Grid Search 进行超参数调优(更方便); * 尽量不要只绑定5个,有时最好的参数可以更大; * 尝试完全使用 ARIMA 模型,而不是 SARIMAX; * 尝试收集更多数据,因此可能会导致错误的预测:(
    【解决方案2】:

    回答您的第一个问题: 您收集的数据看起来很小,如果您可以每天收集数据以使您的模型做得很好,那就太好了。因为,循环神经网络在收集的数据元素的时间差较小时表现良好,我建议您每天收集数据,可以将您带到 (12 x 30 x 6) 它可以成为任何模型的最佳馈送。

    第二个问题的答案: 我个人建议你尝试一下 LSTM,它有更多的数据和有价值的参数,并且在这篇 Medium 帖子中给出了一个很好的集合。Medium Post

    性能随参数的变化而变化,因此在选择输入的参数时要谨慎。

    第三个问题的答案: 通常使用称为“异常检测”的技术来检测季节性。在上面给出的中等帖子中也对此进行了小讨论。

    【讨论】:

    • 原始数据集是按天收集的(大约有 3000 行),我是重新组合它的人,因为我需要按月预测销售额。我已经查看了你说的链接,它非常有用,谢谢,但它没有任何 python 代码,而且我刚开始时编程还不是很好。你有示例代码吗?
    • 好的,我会尽快和你分享一些资源。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    相关资源
    最近更新 更多