【发布时间】: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 没有固定的频率集,但值的集合是随机的。我找到了不同的教程,例如 this、this other one 或 this 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