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 = 77 # 预测的值数量 # 进行单步指数平滑预测 predictions = single_exponential_smoothing(need[:100], 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[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_1.html')我希望在图中显示的实际值从数据的初始开始,一直绘制到177周,同样,预测的也要从第一周开始预测,预测出第一周到第177周的结果并在图中呈现,如果单步指数平滑法的代码不对你可以进行修改,但是要是单步指数平滑法
时间: 2024-04-17 10:26:01 浏览: 92
明白了,你希望在图中显示从数据的初始开始到第177周的实际值预测从第一周到第177的结果。我已经做出了修改:
```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 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')
```
这样,实际值和预测值都会从数据的初始开始绘制到第177周。希望这次修改符合你的要求。如果还有其他问题,请随时告诉我。
阅读全文