Holt-Winters季节性预测模型

2022-05-29 10:35:47 浏览数 (1)

代码语言:javascript复制
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import mean_squared_error
from math import sqrt
from statsmodels.tsa.api import SimpleExpSmoothing
import statsmodels.api as sm
from statsmodels.tsa.api import Holt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False  
df=pd.read_csv('C:/Users/xpp/Desktop/data.csv', nrows=11856)
#创建训练和测试集
train=df[0:10300]
test=df[10300:]
#数据聚合
df['Timestamp']=pd.to_datetime(df['Datetime'],format='%d-%m-%Y %H:%M')#4位年用Y,2位年用y
df.index=df['Timestamp']
df=df.resample('D').mean()#按天采样,计算均值
train['Timestamp']=pd.to_datetime(train['Datetime'],format='%d-%m-%Y %H:%M')
train.index=train['Timestamp']
train=train.resample('D').mean()
test['Timestamp']=pd.to_datetime(test['Datetime'],format='%d-%m-%Y %H:%M')
test.index=test['Timestamp']
test=test.resample('D').mean()
#rms=sqrt(mean_squared_error(test.Count,y_hat_exp_avg.SES))
#print("SES",rms)
#绘图
y_hat_avg=test.copy()
fit1=ExponentialSmoothing(np.asarray(train['Count']),seasonal_periods=7,trend='add',seasonal='add',).fit()
y_hat_avg['Holt_Winter']=fit1.forecast(len(test))
plt.figure(figsize=(16,8))
plt.plot(train['Count'],label='Train')
plt.plot(test['Count'],label='Test')
plt.plot(y_hat_avg['Holt_Winter'],label='Holt_Winter')
plt.legend(loc='best')
plt.show()

算法:Holt-Winters季节性预测模型是一种三次指数平滑预测,除了水平和趋势外,还将指数平滑应用到季节分量上。

文献:张丽, & 闫世锋. (2006). Holt-winters方法与arima模型在中国航空旅客运输量预测中的比较研究. 上海工程技术大学学报, 20(3), 4.

0 人点赞