指数平滑预测所有空值
Exponential Smoothing predicts all null values
我编写了一个程序,应该使用指数平滑来预测值。我有 6 个月的数据(从四月到九月)。基于这 6 个月,我想预测接下来的 6 个月(即从 10 月到 3 月)。
这是我的代码:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from statsmodels.tsa.holtwinters import ExponentialSmoothing
d = {'Month':['April','May','June','July','August','September'],'Value':[2.868,7.205,13.349,20.115,22.769,23.981]}
df = pd.DataFrame(data=d)
model = ExponentialSmoothing(df['Value'],trend='mul',seasonal='mul',seasonal_periods = 6).fit()
predict = model.forecast(6)
然而,当我看到预测值时,它都是 Nans
。我不确定我在哪里犯了错误。谁能帮忙解决这个问题?
查看 documentation,seasonal_periods
是季节性周期中的周期数。
如果您查看 Holt-Winters 的等式(例如 here),它包含一个季节性项,该项移动了 m
步(其中 m
等于 seasonal_periods
)。这意味着,要在 seasonal_periods=6
处进行预测,您需要在数据中提前 6
个时间步获得输入值。
由于数据仅包含 6
个数据点,因此无法进行任何预测。
可能的解决方案:
- 周期为6的数据真的是季节性的吗?如果不是,请删除
seasonal_periods
参数或更改它。
- 向数据框添加更多数据。如果您的数据只有一个时间步长,那么您将获得实际的预测。
增加输入数据点,由于缺乏数据,我猜模型无法收敛并决定趋势和季节性因素。我添加了一个额外的数据点,模型能够决定因素。
As a rule of thumb, a minimum of two full seasons (or 2L periods) of
historical data is needed to initialize a set of seasonal factors.
>>> data
2020-04-01 2.868
2020-05-01 7.205
2020-06-01 13.349
2020-07-01 20.115
2020-08-01 22.769
2020-09-01 23.981
2020-10-01 22.100
Freq: MS, dtype: float64
>>> model = ExponentialSmoothing(data,trend='mul',seasonal='mul', seasonal_periods=6).fit()
>>> model.forecast(10)
2020-11-01 55.519626
2020-12-01 102.863252
2021-01-01 154.999872
2021-02-01 175.450687
2021-03-01 184.789706
2021-04-01 170.295457
2021-05-01 427.816286
2021-06-01 792.630950
2021-07-01 1194.378883
2021-08-01 1351.966249
Freq: MS, dtype: float64
参考:
https://en.wikipedia.org/wiki/Exponential_smoothing#Triple_exponential_smoothing_(Holt_Winters)
我编写了一个程序,应该使用指数平滑来预测值。我有 6 个月的数据(从四月到九月)。基于这 6 个月,我想预测接下来的 6 个月(即从 10 月到 3 月)。
这是我的代码:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from statsmodels.tsa.holtwinters import ExponentialSmoothing
d = {'Month':['April','May','June','July','August','September'],'Value':[2.868,7.205,13.349,20.115,22.769,23.981]}
df = pd.DataFrame(data=d)
model = ExponentialSmoothing(df['Value'],trend='mul',seasonal='mul',seasonal_periods = 6).fit()
predict = model.forecast(6)
然而,当我看到预测值时,它都是 Nans
。我不确定我在哪里犯了错误。谁能帮忙解决这个问题?
查看 documentation,seasonal_periods
是季节性周期中的周期数。
如果您查看 Holt-Winters 的等式(例如 here),它包含一个季节性项,该项移动了 m
步(其中 m
等于 seasonal_periods
)。这意味着,要在 seasonal_periods=6
处进行预测,您需要在数据中提前 6
个时间步获得输入值。
由于数据仅包含 6
个数据点,因此无法进行任何预测。
可能的解决方案:
- 周期为6的数据真的是季节性的吗?如果不是,请删除
seasonal_periods
参数或更改它。 - 向数据框添加更多数据。如果您的数据只有一个时间步长,那么您将获得实际的预测。
增加输入数据点,由于缺乏数据,我猜模型无法收敛并决定趋势和季节性因素。我添加了一个额外的数据点,模型能够决定因素。
As a rule of thumb, a minimum of two full seasons (or 2L periods) of historical data is needed to initialize a set of seasonal factors.
>>> data
2020-04-01 2.868
2020-05-01 7.205
2020-06-01 13.349
2020-07-01 20.115
2020-08-01 22.769
2020-09-01 23.981
2020-10-01 22.100
Freq: MS, dtype: float64
>>> model = ExponentialSmoothing(data,trend='mul',seasonal='mul', seasonal_periods=6).fit()
>>> model.forecast(10)
2020-11-01 55.519626
2020-12-01 102.863252
2021-01-01 154.999872
2021-02-01 175.450687
2021-03-01 184.789706
2021-04-01 170.295457
2021-05-01 427.816286
2021-06-01 792.630950
2021-07-01 1194.378883
2021-08-01 1351.966249
Freq: MS, dtype: float64
参考: https://en.wikipedia.org/wiki/Exponential_smoothing#Triple_exponential_smoothing_(Holt_Winters)