import pandas as pd from pyecharts import options as opts from pyecharts.charts import Line # 读取Excel文件 data = pd.read_excel('6004020918.xlsx') # 提取数据 week = data['week'] need = data['need'] # 定义单步指数平滑函数 def single_exponential_smoothing(series, alpha, n_preds): result = [series[0]] for i in range(1, len(series) + n_preds): if i >= len(series): # 预测新值 m = i - len(series) + 1 result.append(alpha * result[-1] + (1 - alpha) * (result[-1] - result[-2])) else: # 更新指数平滑值 result.append(alpha * series[i] + (1 - alpha) * result[i - 1]) return result # 设置单步指数平滑法参数 alpha = 0.5 n_preds = 177 # 预测的值数量 # 进行单步指数平滑预测 predictions = single_exponential_smoothing(need, alpha, n_preds) # 创建折线图对象 line = Line() line.set_global_opts( title_opts=opts.TitleOpts(title='时间序列预测分析'), legend_opts=opts.LegendOpts(pos_top='5%'), tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross") ) # 添加预测值数据 line.add_xaxis(week) line.add_yaxis('预测值', predictions, is_smooth=True, label_opts=opts.LabelOpts(is_show=False)) # 添加实际值数据 line.add_yaxis('实际值', need, is_smooth=True, label_opts=opts.LabelOpts(is_show=False)) # 生成HTML文件 line.render('time_series_forecast_2.html')将这个预测改为三步指数平滑法预测,其他不变
时间: 2024-04-17 17:26:02 浏览: 237
详解pandas库pd.read_excel操作读取excel文件参数整理与实例
好的,你希望将单步指数平滑法预测改为三步指数平滑法预测。下面是修改后的代码:
```python
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Line
# 读取Excel文件
data = pd.read_excel('6004020918.xlsx')
# 提取数据
week = data['week']
need = data['need']
# 定义三步指数平滑函数
def triple_exponential_smoothing(series, alpha, beta, gamma, n_preds):
result = [series[0]]
for i in range(1, len(series) + n_preds):
if i >= len(series):
# 预测新值
m = i - len(series) + 1
result.append((result[-1] + m * result[-3]) / (m + 1))
else:
# 更新指数平滑值
result.append(alpha * series[i] + (1 - alpha) * (result[i - 1] + result[i - 2]) / 2)
return result
# 设置三步指数平滑法参数
alpha = 0.5
beta = 0.3
gamma = 0.2
n_preds = 177 # 预测的值数量
# 进行三步指数平滑预测
predictions = triple_exponential_smoothing(need, alpha, beta, gamma, n_preds)
# 创建折线图对象
line = Line()
line.set_global_opts(
title_opts=opts.TitleOpts(title='时间序列预测分析'),
legend_opts=opts.LegendOpts(pos_top='5%'),
tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross")
)
# 添加预测值数据
line.add_xaxis(week)
line.add_yaxis('预测值', predictions, is_smooth=True, label_opts=opts.LabelOpts(is_show=False))
# 添加实际值数据
line.add_yaxis('实际值', need, is_smooth=True, label_opts=opts.LabelOpts(is_show=False))
# 生成HTML文件
line.render('time_series_forecast_3.html')
```
这样,预测将使用三步指数平滑法进行。希望这次修改符合你的要求。如果还有其他问题,请随时告诉我。
阅读全文