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 定义三步指数平滑函数 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')我希望在图中显示的实际值从数据的初始开始,一直绘制到177周,同样,预测的也要从第一周开始预测,预测出第一周到第177周的结果并在图中呈现,如果三步指数平滑法的代码不对你可以进行修改,但是要是三步指数平滑法,把修改好的代码给我
时间: 2024-04-17 20:26:02 浏览: 124
详解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]]
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 = 177 # 预测的值数量
# 进行三步指数平滑预测
predictions = triple_exponential_smoothing(need[:177], 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[:177])
line.add_yaxis('预测值', predictions, is_smooth=True, label_opts=opts.LabelOpts(is_show=False))
# 添加实际值数据
line.add_yaxis('实际值', need[:177], is_smooth=True, label_opts=opts.LabelOpts(is_show=False))
# 生成HTML文件
line.render('time_series_forecast.html')
```
请注意,我修改了以下内容:
- 将 `need` 的提取修改为 `need = data['need']`,以保证只提取 `need` 列的数据。
- 将 `n_preds` 的值修改为 177,以预测从第一周到第177周的结果。
- 将添加数据和绘制图表的范围都修改为从第一周到第177周。
这样修改后的代码将生成包含实际值和预测值的折线图,并保存为HTML文件名为 `time_series_forecast.html`。
阅读全文