当前位置: 首页 > 后端技术 > Python

将梯度提升模型与 Prophet 相结合可以提升时间序列预测的效果

时间:2023-03-26 14:58:56 Python

undefinedundefinedns=pd.concat([predictions_train,predictions_test],axis=0)returnpredictions上面的函数会为我们的LightGBM模型准备的新特征返回一个DF:使用ProphetfeaturestotrainAutorregressiveLightGBM我们使用Prophet来提取新特征,下一步是使用LightGBM组合特征和预测:deftrain_time_series_with_folds_autoreg_prophet_features(df,horizo??n=24*7,lags=[1,2,3,4,5]):#createadataframewiththeallthenewfeaturescreatedwithProphetnew_prophet_features=prophet_features(df,horizo??n=horizo??n)df.reset_index(inplace=True)#将Prophet特征数据框与我们的第一个数据框合并df=pd.merge(df,new_prophet_features,left_on=['datetime'],right_on=['ds'],how='inner')df.drop('ds',axis=1,inplace=True)df.set_index('datetime',inplace=True)#createsomelagvariablesusingProphetpredictions(yhat列)对于滞后滞后:df[f'yhat_lag_{lag}']=df['yhat'].shift(lag)df.dropna(axis=0,how='any')X=df.drop('count',轴=1)y=df['count']#取上周的数据集进行验证X_train,X_test=X.iloc[:-horizo??n,:],X.iloc[-horizo??n:,:]y_train,y_test=y.iloc[:-horizo??n],y.iloc[-horizo??n:]#defineLightGBM模型,训练它并做出预测model=LGBMRegressor(random_state=42)model.fit(X_train,y_train)predictions=model.predict(X_test)#calculateMAEmae=np.round(mean_absolute_error(y_test,predictions),3)#plotrealityvspredictionfor最后一周的数据集fig=plt.figure(figsize=(16,6))plt.title(f'RealvsPrediction-MAE{mae}',fontsize=20)plt.plot(y_test,color='red')plt.plot(pd.Series(predictions,index=y_test.index),color='green')plt.xlabel('Hour',fontsize=16)plt.ylabel('NumberofSharedBikes',fontsize=16)plt.legend(labels=['Real','Prediction'],fontsize=16)plt.grid()plt.show()执行以上代码后,我们将合并特征df,创建lag的滞后值,训练LightGBM模型,然后用我们训练好的模型进行预测,将我们的预测与实际结果undefined