指数平滑预测所有空值

2024-04-18 22:53:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经写了一个程序,应该使用指数平滑法预测值。我有6个月的数据(从4月到9月)。基于这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。我不确定我在哪里犯了错误。有人能帮忙纠正这个问题吗


Tags: 数据import程序密码pandasdfmodelvalue
2条回答

增加输入数据点,由于缺乏数据,我猜模型无法收敛并决定趋势和季节因素。我添加了一个额外的数据点,模型能够确定这些因素

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)

看看documentationseasonal_periods是季节周期中的周期数

如果你看霍尔特-温特斯方程(例如,here),它包括一个季节性的术语,其移动了m步(其中m等于seasonal_periods)。这意味着,要在seasonal_periods=6处进行预测,需要在数据6时间步中提前输入值

由于数据只包含6个数据点,因此无法进行任何预测

可能的解决办法:

  • 数据是否真的是周期为6的季节性数据?如果不是,请删除seasonal_periods参数或对其进行更改
  • 向数据帧添加更多数据。如果您只有一个多时间步的数据,那么您将获得实际的预测

相关问题 更多 >