4

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

在此处输入图像描述

现在我想实现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

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

如果有人想重现结果,这是本月的数据

                                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
4

1 回答 1

0

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

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

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

于 2018-05-15T09:21:13.997 回答