You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中如何拟合Holt Winter模型并预测未来5年销售额

Holt-Winter未来5年销售额预测解决方案

你已经通过测试集验证了当前Holt-Winter模型的参数适配你的销售数据集,接下来只需调整以下两步即可完成5年预测:

  1. 用全部4年的历史销售数据重新训练模型(拆分训练集/测试集仅用于验证参数有效性,正式预测需要用全量数据保证拟合效果最优)
  2. 设置预测步长为60(5年共60个月),同时生成对应未来时间索引即可

完整可运行代码

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.api import ExponentialSmoothing

# ------------ 1. 用全量历史数据训练最终模型 ------------
# full_data为你完整的4年销售数据集,需确保其索引为datetime时间格式
holt_winter_final = ExponentialSmoothing(
    np.asarray(full_data['Sales']),
    seasonal_periods=12,
    trend='add',
    seasonal='add'
)
hw_fit_final = holt_winter_final.fit()

# ------------ 2. 生成未来5年的时间索引 ------------
# 从现有数据的最晚时间点开始生成60个月的时间序列
# freq参数根据你的数据时间粒度调整:MS为每月第一天,M为每月最后一天
future_dates = pd.date_range(start=full_data.index.max(), periods=60, freq='MS')

# ------------ 3. 执行未来5年预测 ------------
future_forecast = hw_fit_final.forecast(steps=60)

# ------------ 4. 结果可视化与导出 ------------
# 绘制历史数据+预测结果
plt.figure(figsize=(16,8))
plt.plot(full_data.index, full_data['Sales'], "b.-", label='历史销售数据')
plt.plot(future_dates, future_forecast, "gx-", label='未来5年预测销售数据')
plt.ylabel('销售额', fontsize=16)
plt.xlabel('时间', fontsize=16)
plt.legend(loc='best')
plt.title('Holt Winters 5年销售额预测', fontsize=20)
plt.show()

# 导出预测结果为DataFrame方便后续处理
forecast_result = pd.DataFrame({
    '日期': future_dates,
    '预测销售额': future_forecast
})
print(forecast_result)

注意事项

  • 如果测试集验证时发现乘性趋势/季节性拟合效果更好,直接替换trend和seasonal参数为mul即可
  • 可运行print(hw_fit_final.summary())查看模型拟合的详细指标和参数
  • 如果你的数据不是月度粒度,调整seasonal_periods和pd.date_range的freq参数匹配你的数据周期即可

内容的提问来源于stack exchange,提问作者S. Mandal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 18:09:02