ML之VAR:基于上海最高气温数据集利用时间序列模型之VAR向量自回归模型/多变量自回归模型实现回归预测案例
相关文章
ML之VAR:基于上海最高气温数据集利用时间序列模型之VAR向量自回归模型/多变量自回归模型实现回归预测案例
ML之VAR:基于上海最高气温数据集利用时间序列模型之VAR向量自回归模型/多变量自回归模型实现回归预测案例实现代码
week max_temperature ... air_quality_index air_quality_level
date ...
2021-01-01 周五 4 ... 52.0 良
2021-01-02 周六 7 ... 69.0 良
2021-01-03 周日 10 ... 66.0 良
2021-01-04 周一 13 ... 44.0 优
2021-01-05 周二 8 ... 49.0 优
... ... ... ... ... ...
2023-03-12 周日 12 ... 68.0 良
2023-03-13 周一 14 ... 52.0 良
2023-03-14 周二 20 ... 55.0 良
2023-03-15 周三 23 ... 52.0 良
2023-03-16 周四 15 ... 69.0 良
[805 rows x 8 columns]
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 805 entries, 2021-01-01 to 2023-03-16
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 week 805 non-null object
1 max_temperature 805 non-null int64
2 min_temperature 805 non-null int64
3 weather 805 non-null object
4 wind_direction 805 non-null object
5 wind_level 805 non-null object
6 air_quality_index 667 non-null float64
7 air_quality_level 775 non-null object
dtypes: float64(1), int64(2), object(5)
memory usage: 56.6+ KB
对时间序列数据进行向填充
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 805 entries, 2021-01-01 to 2023-03-16
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 max_temperature 805 non-null int64
1 min_temperature 805 non-null int64
2 air_quality_index 805 non-null float64
dtypes: float64(1), int64(2)
memory usage: 25.2 KB
after select None
(764, 3)
lag_order 1
(1, 3)
# 获取了已拟合(trained)的VAR模型对象的滞后阶数(lag order)。滞后阶数是指在多变量自回归模型中,用来预测当前时刻的变量值所使用的过去观测值的数量。
# 定义预测样本个数
# 输出多变量预测结果
max_temperature min_temperature air_quality_index
0 7.985491 2.331710 55.248152
1 8.271345 2.674797 57.089048
2 8.637829 3.020773 58.096087
3 9.017162 3.364621 58.597781
4 9.389668 3.703368 58.790258
……
30 15.907383 9.875085 53.593724
31 16.069888 10.029798 53.441149
32 16.227586 10.179934 53.293086
33 16.380618 10.325628 53.149404
34 16.529124 10.467013 53.009971
35 16.673237 10.604216 52.874663
36 16.813086 10.737359 52.743358
37 16.948798 10.866564 52.615937
38 17.080496 10.991947 52.492285
39 17.208298 11.113621 52.372291
40 17.332319 11.231695 52.255847
max_temperature
VAR_model_val_RMSE: nan
VAR_model_val_MSE: 20.01365459529838
VAR_model_val_R2: 0.389065276118679
min_temperature
VAR_model_val_RMSE: nan
VAR_model_val_MSE: 10.518481162716537
VAR_model_val_R2: -0.10454565433074081
air_quality_index
VAR_model_val_RMSE: nan
VAR_model_val_MSE: 498.12373855288257
VAR_model_val_R2: -0.1276536779817219
联系客服