如何拟合 Holt Winter 的模型并预测 Python 中的未来结果?
How to fit Holt Winter’s model and forecast future outcomes in Python?
我有一个包含 4 年销售额的数据集,并试图预测未来五年的销售额。我将数据集分为 36 个月作为训练集和 12 个月作为测试集。我选择了Holt Winter的方法,写了下面的代码来测试模型。
from statsmodels.tsa.api import ExponentialSmoothing
holt_winter = ExponentialSmoothing(np.asarray(train_data['Sales']), seasonal_periods=12, trend='add', seasonal='add')
hw_fit = holt_winter.fit()
hw_forecast = hw_fit.forecast(len(test_data))
plt.figure(figsize=(16,8))
plt.plot(train_data.index, train_data['Sales'], "b.-", label='Train Data')
plt.plot(test_data.index, test_data['Sales'], "ro-", label='Original Test Data')
plt.plot(test_data.index, hw_forecast, "gx-", label='Holt_Winter Forecast Data')
plt.ylabel('Score', fontsize=16)
plt.xlabel('Time', fontsize=16)
plt.legend(loc='best')
plt.title('Holt Winters Forecast', fontsize=20)
plt.show()
代码似乎运行良好,并且可能正确预测了测试数据集的结果。但是,如果我想预测未来五年的销售额,我正在努力弄清楚如何编码?
hw_fit.predict(start, end)
将从步骤开始到步骤结束进行预测,步骤0是训练数据的第一个值。
forecast 进行样本外预测。所以这两个是等价的:
hw_fit.forecast(steps)
hw_fit.predict(len(train_data), len(train_data)+steps-1)
所以,由于你的模型是按月训练的,如果你想预测 n 个月后的训练数据,你可以用 [=16= 调用上面的方法]步数=n
您也可以尝试 ARIMA 模型,它通常会提供更好的性能,并且此代码组合了不同的 ARIMA 参数(AR,自回归参数;I,差分参数;MA,移动平均参数;-p,d, q 分别)并通过降低 Akaike 信息标准 (AIK) 来找到它们的最佳组合,AIK 用参数数量来惩罚最大似然(即找到最佳似然,参数数量最少):
from statsmodels.tsa.arima_model import ARIMA
import itertools
# Grid Search
p = d = q = range(0,3) # p, d, and q can be either 0, 1, or 2
pdq = list(itertools.product(p,d,q)) # gets all possible combinations of p, d, and q
combs = {} # stores aic and order pairs
aics = [] # stores aics
# Grid Search continued
for combination in pdq:
try:
model = ARIMA(train_data['Sales'], order=combination) # create all possible models
model = model.fit()
combs.update({model.aic : combination}) # store combinations
aics.append(model.aic)
except:
continue
best_aic = min(aics)
我有一个包含 4 年销售额的数据集,并试图预测未来五年的销售额。我将数据集分为 36 个月作为训练集和 12 个月作为测试集。我选择了Holt Winter的方法,写了下面的代码来测试模型。
from statsmodels.tsa.api import ExponentialSmoothing
holt_winter = ExponentialSmoothing(np.asarray(train_data['Sales']), seasonal_periods=12, trend='add', seasonal='add')
hw_fit = holt_winter.fit()
hw_forecast = hw_fit.forecast(len(test_data))
plt.figure(figsize=(16,8))
plt.plot(train_data.index, train_data['Sales'], "b.-", label='Train Data')
plt.plot(test_data.index, test_data['Sales'], "ro-", label='Original Test Data')
plt.plot(test_data.index, hw_forecast, "gx-", label='Holt_Winter Forecast Data')
plt.ylabel('Score', fontsize=16)
plt.xlabel('Time', fontsize=16)
plt.legend(loc='best')
plt.title('Holt Winters Forecast', fontsize=20)
plt.show()
代码似乎运行良好,并且可能正确预测了测试数据集的结果。但是,如果我想预测未来五年的销售额,我正在努力弄清楚如何编码?
hw_fit.predict(start, end)
将从步骤开始到步骤结束进行预测,步骤0是训练数据的第一个值。
forecast 进行样本外预测。所以这两个是等价的:
hw_fit.forecast(steps)
hw_fit.predict(len(train_data), len(train_data)+steps-1)
所以,由于你的模型是按月训练的,如果你想预测 n 个月后的训练数据,你可以用 [=16= 调用上面的方法]步数=n
您也可以尝试 ARIMA 模型,它通常会提供更好的性能,并且此代码组合了不同的 ARIMA 参数(AR,自回归参数;I,差分参数;MA,移动平均参数;-p,d, q 分别)并通过降低 Akaike 信息标准 (AIK) 来找到它们的最佳组合,AIK 用参数数量来惩罚最大似然(即找到最佳似然,参数数量最少):
from statsmodels.tsa.arima_model import ARIMA
import itertools
# Grid Search
p = d = q = range(0,3) # p, d, and q can be either 0, 1, or 2
pdq = list(itertools.product(p,d,q)) # gets all possible combinations of p, d, and q
combs = {} # stores aic and order pairs
aics = [] # stores aics
# Grid Search continued
for combination in pdq:
try:
model = ARIMA(train_data['Sales'], order=combination) # create all possible models
model = model.fit()
combs.update({model.aic : combination}) # store combinations
aics.append(model.aic)
except:
continue
best_aic = min(aics)