python-3.x - 无法使用 statsmodels 库实现 Holt-Winters 方法

标签 python-3.x pandas forecasting statsmodels holtwinters

我有一个每天的一个月数据。它捕获 cpu utilization每天的数据。我想产生一些预测结果。我把数据分成两部分train - 第一个 15 天和test取最后一个 16 天,在此我想做一个预测并将预测结果与给定的最后 进行比较16 天结果。到目前为止,我已经尝试了各种实现,例如 moving average , simple exponential smoothing .现在我想尝试一些更复杂和准确的东西,比如Holt-Winters MethodARIMA model .下面是我得到的结果 Holt's Linear Trend考虑趋势和季节性的方法。

enter image description here

现在我想实现 Holts Winter method这是首选的预测技术之一。这是下面的代码

# get the first 15 days
df_train = psql.read_sql("SELECT date,cpu FROM {} where date between '{}' and '{} 23:59:59';".format(conf_list[1], '2018-03-02', '2018-03-16'), conn).fillna(0)
df_train["date"] = pd.to_datetime(df_train["date"], format="%m-%d-%Y")
df_train.set_index("date", inplace=True)
df_train = df_train.resample('D').mean().fillna(0)

# get the last 15 days
df_test = psql.read_sql("SELECT date,cpu FROM {} where date between '{}' and '{} 23:59:59';".format(conf_list[1], '2018-03-18', '2018-03-31'), conn).fillna(0)
df_test["date"] = pd.to_datetime(df_test["date"], format="%m-%d-%Y")
df_test.set_index("date", inplace=True)
df_test = df_test.resample('D').mean().fillna(0)

这是Holt's Winter method的代码
y_hat_avg = df_test.copy()
fit1 = ExponentialSmoothing(np.asarray(df_train['cpu']), seasonal_periods=1, trend='add', seasonal='add',).fit()
y_hat_avg['Holt_Winter'] = fit1.forecast(len(df_test))
plt.figure(figsize=(16,8))
plt.plot(df_train['cpu'], label='Train')
plt.plot(df_test['cpu'], label='Test')
plt.plot(y_hat_avg['Holt_Winter'], label='Holt_Winter')
plt.legend(loc='best')
plt.show()

现在我收到 seasonal_periods 的错误参数。它接受一个整数,我相信它接受月份作为一个值。即使在他们的文档中,他们也只提到没有季节 http://www.statsmodels.org/dev/generated/statsmodels.tsa.holtwinters.ExponentialSmoothing.html#statsmodels.tsa.holtwinters.ExponentialSmoothing

现在因为我只有 1 我想在第一个 上运行预测的数据的月份15 天,我应该通过什么季节值?假设季节是指月份,理想情况下应该是 0.5 (15 天),但它只接受整数。如果我将值传递为 1 ,我收到以下错误
Traceback (most recent call last):
  File "/home/souvik/PycharmProjects/Pandas/forecast_health.py", line 89, in <module>
    fit1 = ExponentialSmoothing(np.asarray(df_train['cpu']), seasonal_periods=1, trend='add', seasonal='add',).fit()
  File "/home/souvik/data_analysis/lib/python3.5/site-packages/statsmodels/tsa/holtwinters.py", line 571, in fit
    Ns=20, full_output=True, finish=None)
  File "/home/souvik/data_analysis/lib/python3.5/site-packages/scipy/optimize/optimize.py", line 2831, in brute
    Jout = vecfunc(*grid)
  File "/home/souvik/data_analysis/lib/python3.5/site-packages/numpy/lib/function_base.py", line 2755, in __call__
    return self._vectorize_call(func=func, args=vargs)
  File "/home/souvik/data_analysis/lib/python3.5/site-packages/numpy/lib/function_base.py", line 2831, in _vectorize_call
    outputs = ufunc(*inputs)
  File "/home/souvik/data_analysis/lib/python3.5/site-packages/scipy/optimize/optimize.py", line 2825, in _scalarfunc
    return func(params, *args)
  File "/home/souvik/data_analysis/lib/python3.5/site-packages/statsmodels/tsa/holtwinters.py", line 207, in _holt_win_add_add_dam
    return sqeuclidean((l + phi * b) + s[:-(m - 1)], y)
ValueError: operands could not be broadcast together with shapes (16,) (0,)

如果我将参数传递为 None ,我收到以下错误
Traceback (most recent call last):
  File "/home/souvik/PycharmProjects/Pandas/forecast_health.py", line 89, in <module>
    fit1 = ExponentialSmoothing(np.asarray(df_train['cpu']), seasonal_periods=None, trend='add', seasonal='add',).fit()
  File "/home/souvik/data_analysis/lib/python3.5/site-packages/statsmodels/tsa/holtwinters.py", line 399, in __init__
    'Unable to detect season automatically')
NotImplementedError: Unable to detect season automatically

我如何获得最后一次的预测 16 使用 Holt-Winters 方法一个月的几天?我做错了什么?

如果有人想重现结果,这里是当月的数据
                                cpu
date                               
2018-03-01 00:00:00+00:00  1.060606
2018-03-02 00:00:00+00:00  1.014035
2018-03-03 00:00:00+00:00  1.048611
2018-03-04 00:00:00+00:00  1.493392
2018-03-05 00:00:00+00:00  3.588957
2018-03-06 00:00:00+00:00  2.500000
2018-03-07 00:00:00+00:00  5.265306
2018-03-08 00:00:00+00:00  0.000000
2018-03-09 00:00:00+00:00  3.062099
2018-03-10 00:00:00+00:00  5.861751
2018-03-11 00:00:00+00:00  0.000000
2018-03-12 00:00:00+00:00  0.000000
2018-03-13 00:00:00+00:00  7.235294
2018-03-14 00:00:00+00:00  4.011662
2018-03-15 00:00:00+00:00  3.777409
2018-03-16 00:00:00+00:00  5.754559
2018-03-17 00:00:00+00:00  4.273390
2018-03-18 00:00:00+00:00  2.328782
2018-03-19 00:00:00+00:00  3.106048
2018-03-20 00:00:00+00:00  5.584877
2018-03-21 00:00:00+00:00  9.869841
2018-03-22 00:00:00+00:00  5.588215
2018-03-23 00:00:00+00:00  3.620377
2018-03-24 00:00:00+00:00  3.468021
2018-03-25 00:00:00+00:00  2.605649
2018-03-26 00:00:00+00:00  3.670559
2018-03-27 00:00:00+00:00  4.071777
2018-03-28 00:00:00+00:00  4.159690
2018-03-29 00:00:00+00:00  4.364939
2018-03-30 00:00:00+00:00  4.743253
2018-03-31 00:00:00+00:00  4.928571

最佳答案

首先,错误NotImplementedError: Unable to detect season automatically显示是因为您已定义 seasonal_periods作为 None ,但你仍然有参数 seasonaladd ,您应该将其更改为 None。

如果您的数据具有每月季节性,而您只有一个月,那么您的样本中可能根本没有季节性。但如果您愿意,您可以通过绘制数据的傅立叶变换以寻找季节性来检查它。

另外,我相信对于预测(在我从您的示例中看到的样本中),如果您使用的是 Statsmodels,那么最好使用 predict forecast 的内部,它们在许多情况下产生不同的结果。

关于python-3.x - 无法使用 statsmodels 库实现 Holt-Winters 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50346143/

相关文章:

python - 在 pandas python 中将列值转换为行值时出现问题?

python - 从 pandas DataFrame 中高效扩展行

python - Pandas 检查列中 x 的子字符串,如果找到将字符串添加到 x

python - ARMA.predict 的预测区间

r - 如何计算残差的相关性? Spark-Scala

优化算法与回归模型

python - 无法安装zbar

python - 是否可以在通用创建 View 表单中添加占位符?

python - python 中的 docopt 给我带来了问题

python - Pandas 中更快的应用方法