【问题标题】:Predict with Triple Exponential Smoothing using ExponentialSmoothing of statsmodels in python在 python 中使用 statsmodels 的 ExponentialSmoothing 进行三次指数平滑预测
【发布时间】:2019-02-26 07:05:58
【问题描述】:

我正在对一些时间序列进行预测,我需要使用 python 比较不同的方法。实际上,我需要使用三重指数平滑来生成一些预测,并且我正在使用this library 和像this 这样的相关函数。 我的时间序列有这种格式,作为 pd.Series 对象:

    Date Close
2016-04-11 01:17:04    -10.523793
2016-04-11 07:25:13     -5.352295
2016-04-11 22:40:11     92.556003
2016-04-13 05:06:31     -1.769866
2016-04-13 05:17:50     -2.330789
2016-04-14 08:43:09     17.636638
2016-04-17 21:15:12     -0.454655
2016-04-19 06:10:04     -0.026375
2016-04-19 06:10:04     -0.175647
...

我在 python 中写了这些行:

from statsmodels.tsa.holtwinters import ExponentialSmoothing
from matplotlib import pyplot as plt
import numpy as np 
import pandas as pd
train_size = int(len(myTimeSeries) * 0.66)
train, test = myTimeSeries[1:train_size], myTimeSeries[train_size:]

model = ExponentialSmoothing(train)
model_fit = model.fit()
dict=model.params
params=np.array(list(dict.items()))
dates=test.index.astype(str)
pred = model.predict(params,start=dates[2], end=dates[-1])
plt.plot(train, label='Train')
plt.plot(test, label='Test')
plt.plot(pred, label='Holt-Winters')
plt.legend(loc='best')

我遇到了函数model.predict 的问题,所以我根据需要添加了参数值,从model 类中获取它们,在它的fit 之后。我不确定我是否做得很好,但我没有找到太多around。此外,我在设置开始日期(也可能是结束日期)时遇到了问题。它返回:KeyError: 'Thestartargument could not be matched to a location related to the index of the data.' 正如我发现的here,我还将预测的开始日期移动到第三个值,即测试数据集的索引 [2]。如果我设置 [0]、[1] 等,我会得到相同的结果……这有什么问题?

如您所见,myTimeSeries 没有固定的频率集,但值的集合是随机的。我找到了不同的教程,例如 thisthis other onethis about theory,但它们的条件不同:我不知道关于我的数据集的任何新闻(趋势、季节性频率等) .我没有发现任何违反的假设:如果我错了,请警告我。我曾经考虑过我的理论指南,我发现了here。此外,this post 涵盖了类似的问题,但并不完全相同。

【问题讨论】:

  • model.fit() 你适合什么?您必须将训练 x 和 y 数据传递给它。另外你为什么从myTimeSeries[1:train_size]中的索引`开始?索引从 python 中的0 开始。您应该将其替换为 myTimeSeries[:train_size]
  • 也将model_fit = model.fit()替换为model.fit()
  • @Bazingaa,没有任何变化。关于model_fit = model.fit()它返回拟合参数的对象,你可以在某个地方保存或不保存,比如model_fit 关于myTimeSeries[:train_size]它可以是对的,但它并没有解决真正的问题
  • model.fit() 没有任何参数是不正确的。写 model.fit() 然后 model.predict(params,start=dates[2], end=dates[-1]) 没有任何意义
  • 好的,但解决方案是什么?试想一下:如果我运行model_fit = model.fit()dict=model.params,那么 dict 包含这个{'damping_slope': nan, 'initial_level': 1.6674941946357755, 'initial_seasons': array([], dtype=float64), 'initial_slope': nan, 'lamda': None, 'remove_bias': False, 'smoothing_level': 0.0, 'smoothing_seasonal': nan, 'smoothing_slope': nan, 'use_boxcox': False} ,但是如果我不运行fit() 函数,则不会生成任何参数,实际上它会返回:` AttributeError: 'ExponentialSmoothing' 对象没有属性 'params' `

标签: python pandas time-series statsmodels


【解决方案1】:

我想也许你只是想要:

pred = model_fit.forecast(len(test))

您不能在此处使用日期来指定预测周期,因为您的日期索引没有与之关联的频率,因此您能做的最好的就是指明您想要的预测数量。

【讨论】:

  • 好的,但是如何将生成的数据与训练和测试样本进行排列?如果他们有日期,而pred 没有,我如何在情节中比较它们?谢谢
  • pred 是一个 numpy 数组,因此您可以根据需要将其转换为具有与test 相同索引的 Pandas 数组。 (如pred = pd.Series(model_fit.forecast(len(test)), index=test.index).
  • 好的,即使正确的命令是:pred = pd.Series(model_fit.forecast(len(test)).values, index=test.index)
  • 我得到了这个:screenshots.firefox.com/OLbqQpw9ub3bCSDP/labs.cognitiveclass.ai 你认为这是一个好的预测吗?我看不到预测值(图中的绿色)有任何上升或下降的峰值,因为测试值有(图中的橙色)
猜你喜欢
  • 2023-01-16
  • 2019-02-04
  • 1970-01-01
  • 2016-12-08
  • 2021-03-19
  • 1970-01-01
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多