[数据挖掘] 47 时间序列预测 6 平滑法-简单指数平滑法 美国铁路(美铁)客运公司数据_哔哩哔哩_bilibili
# 将简单指数平滑法应用于通过回归模型得到的残差序列(去除了趋势和季节性效应)
residual_ts = ridership_lm_trendseason.resid # ridership_lm_trendseason.resid 获取回归模型的残差序列
# print(residual_ts)
residual_pred = test_df.Ridership - ridership_lm_trendseason.predict(test_df)
fig,ax = plt.subplots(figsize = (9,4),dpi=180)
ridership_lm_trendseason.resid.plot(ax=ax, color='black',linewidth=0.5)
residual_pred.plot(ax=ax,color='black',linewidth=0.5,style='--')
ax.set_xlabel('Time')
ax.set_ylabel('Ridership')
ax.axhline(y=0,xmin=0,xmax=1,color='grey',linewidth=0.5)
expsmoothFit = ExponentialSmoothing(residual_ts,freq='MS').fit(smoothing_level=0.2) # 对残差序列进行指数平滑的拟合
expsmoothFit.fittedvalues.plot(ax=ax,color='yellowgreen') # 得到拟合值序列并绘图
expsmoothFit.forecast(len(test_ts)).plot(ax=ax,style='--',linewidth=2,color='yellowgreen') # 得到长度为训练集的预测值
singleGraphLayout(ax,[-500,500],train_df,test_df)
ridership_prediction = ridership_lm_trendseason.predict(test_df.iloc[0,:])
print(ridership_prediction[0])
print(expsmoothFit.forecast())
print("简单指数平滑2001-04修正后预测值",ridership_prediction[0]+expsmoothFit.forecast())
print('简单指数平滑2001-04真实值',test_ts[0])
plt.show()