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]] season_length = len(series) // n_preds # 初始化水平、趋势和季节性指数 level, trend, season = series[0], series[1] - series[0], sum(series[:season_length]) / season_length for i in range(1, len(series) + n_preds): if i >= len(series): # 预测新值 m = i - len(series) + 1 result.append(level + m * trend + season) else: # 更新水平、趋势和季节性指数 value = series[i] last_level, level = level, alpha * (value - season) + (1 - alpha) * (level + trend) trend = beta * (level - last_level) + (1 - beta) * trend season = gamma * (value - level) + (1 - gamma) * season result.append(level + trend + season) return result # 设置三步指数平滑法参数 alpha = 0.2 beta = 0.3 gamma = 0.4 n_preds = 77 # 预测的值数量 # 进行三步指数平滑预测 predictions = triple_exponential_smoothing(need[:100], 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[100:177]) line.add_yaxis('预测值', predictions, is_smooth=True, label_opts=opts.LabelOpts(is_show=False)) # 添加实际值数据 line.add_yaxis('实际值', need[100:177], is_smooth=True, label_opts=opts.LabelOpts(is_show=False)) # 生成HTML文件 line.render('time_series_forecast.html')把这个代码中的方法改为两步指数平滑法
时间: 2024-04-17 22:28:27 浏览: 138
你可以将代码中的 `triple_exponential_smoothing` 方法改为 `double_exponential_smoothing` 方法来实现两步指数平滑法。以下是修改后的代码示例:
```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 double_exponential_smoothing(series, alpha, beta, n_preds):
result = [series[0]]
# 初始化水平和趋势指数
level, trend = series[0], series[1] - series[0]
for i in range(1, len(series) + n_preds):
if i >= len(series):
# 预测新值
m = i - len(series) + 1
result.append(level + m * trend)
else:
# 更新水平和趋势指数
value = series[i]
last_level, level = level, alpha * value + (1 - alpha) * (level + trend)
trend = beta * (level - last_level) + (1 - beta) * trend
result.append(level + trend)
return result
# 设置两步指数平滑法参数
alpha = 0.2
beta = 0.3
n_preds = 77 # 预测的值数量
# 进行两步指数平滑预测
predictions = double_exponential_smoothing(need[:100], alpha, beta, 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[100:177])
line.add_yaxis('预测值', predictions, is_smooth=True, label_opts=opts.LabelOpts(is_show=False))
# 添加实际值数据
line.add_yaxis('实际值', need[100:177], is_smooth=True, label_opts=opts.LabelOpts(is_show=False))
# 生成HTML文件
line.render('time_series_forecast.html')
```
这样,你就将三步指数平滑法修改为了两步指数平滑法。
阅读全文