📈 "下个月预计能卖多少?"
老板问这个问题时,Excel 的趋势线只能给你一条直线...
但销售是有季节性的!
✨ 今天教大家:用 Python 做时间序列预测,考虑季节性、趋势、节假日因素。(不会 Python 的同事,文末有 Excel 简化版)
📊 一、为什么 Excel 不够用?
📌 1.1 Excel 能做的
✅ 线性趋势预测 ✅ 移动平均 ✅ 简单指数平滑
📌 1.2 Excel 搞不定的
⚠️ 局限性:
• ❌ 季节性波动(双 11、春节)
• ❌ 多个周期叠加(周周期 + 月周期)
• ❌ 自动选择最优模型
• ❌ 预测区间(置信度)
🚀 二、Python 方案(推荐)
📌 2.1 安装库
pip install pandas matplotlib statsmodels📌 2.2 完整代码
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.holtwinters import ExponentialSmoothing
# 1. 读取数据
df = pd.read_excel('历史销售数据.xlsx', parse_dates=['日期'])
df.set_index('日期', inplace=True)
# 2. 时间序列分解
from statsmodels.tsa.seasonal import seasonal_decompose
decomposition = seasonal_decompose(df['销售额'], model='additive', period=7)
# 3. Holt-Winters 指数平滑
model = ExponentialSmoothing(
df['销售额'],
trend='add',
seasonal='add',
seasonal_periods=7
)
fitted = model.fit()
# 4. 预测未来 30 天
forecast = fitted.forecast(30)
# 5. 可视化并保存
plt.figure(figsize=(14, 6))
plt.plot(df.index[-90:], df['销售额'][-90:], label='历史数据')
plt.plot(forecast.index, forecast, label='预测', color='red', linestyle='--')
plt.savefig('销售预测.png', dpi=300)
# 6. 导出预测结果
forecast_df = pd.DataFrame({'预测销售额': forecast})
forecast_df.to_excel('未来 30 天销售预测.xlsx')
# 7. 汇总统计
print(f'\n未来 30 天总销售额预测:{forecast.sum():,.0f} 元')
print(f'日均销售额预测:{forecast.mean():,.0f} 元')✅ 运行结果:
未来 30 天总销售额预测:750,000 元
日均销售额预测:25,000 元
最高日预测:35,000 元
最低日预测:18,000 元
📌 2.3 模型解读
📊 三、Excel 简化版
📌 3.1 移动平均
// 7 日移动平均(C8 单元格)
=AVERAGE(B2:B8)📌 3.2 指数平滑
// 平滑系数α=0.3
// C2 = B2(第 1 天)
// C3 = 0.3*B3 + 0.7*C2📌 3.3 FORECAST 函数
// 预测第 731 天
=FORECAST(731, B:B, A:A)⚠️ Excel 局限性:
• 只能预测单点,不能预测区间
• 无法考虑季节性
• 数据量大时计算慢
📋 四、总结
📌 4.1 方案对比
💡 关键收获:
1. Python Holt-Winters 模型考虑季节性
2. 时间序列分解(趋势 + 季节 + 残差)
3. 预测区间评估风险
4. Excel 简化版备选方案
📢 下周预告:明天讲交互式看板,纯 Excel 切片器 + 条件格式,不用编程!
❓ 互动话题:你们怎么做销售预测?欢迎留言交流!
本文工具:Python 3.8+(pandas, statsmodels, matplotlib) | Excel 2016+(简化版)
难度:⭐⭐⭐⭐☆(高级) | 预计耗时:Python 版 20 分钟,Excel 版 10 分钟
喜欢本文请点赞 + 在看,分享给更多需要的朋友!
夜雨聆风