赞
踩
DL之LSTM/GRU/CNN:基于tensorflow框架分别利用LSTM/GRU、CNN算法对上海最高气温(构造有监督数据集)实现回归预测案例
目录
基于tensorflow框架分别利用LSTM/GRU、CNN算法对上海最高气温(数据归一化+构造有监督数据集)实现回归预测案例
# 3.1、(构造有监督数据集)切分数据集将训练集和测试集,并转换为LSTM模型所需的数据格式
# 3.2、将数据转换为LSTM/GRU、CNN所需要的3D格式
相关文章
DL之LSTM/GRU/CNN:基于tensorflow框架分别利用LSTM/GRU、CNN算法对上海最高气温(数据归一化+构造有监督数据集)实现回归预测案例
DL之LSTM/GRU/CNN:基于tensorflow框架分别利用LSTM/GRU、CNN算法对上海最高气温(数据归一化+构造有监督数据集)实现回归预测案例实现代码
date | week | max_temperature | min_temperature | weather | wind_direction | wind_level | air_quality_index | air_quality_level |
2021/1/1 | 周五 | 4 | -1 | 晴~多云 | 西北风 | 2级 | 52 | 良 |
2021/1/2 | 周六 | 7 | 1 | 晴~多云 | 东北风 | 2级 | 69 | 良 |
2021/1/3 | 周日 | 10 | 6 | 阴 | 东北风 | 2级 | 66 | 良 |
2021/1/4 | 周一 | 13 | 7 | 阴 | 东风 | 2级 | 44 | 优 |
2021/1/5 | 周二 | 8 | 2 | 阴~多云 | 东北风 | 3级 | 49 | 优 |
2021/1/6 | 周三 | 5 | -4 | 阴 | 北风 | 3级 | 46 | 优 |
2021/1/7 | 周四 | -3 | -6 | 阴 | 西北风 | 4级 | 67 | 良 |
2021/1/8 | 周五 | -1 | -5 | 阴~晴 | 西北风 | 3级 | 50 | 优 |
2021/1/9 | 周六 | 3 | -1 | 晴~多云 | 西北风 | 3级 | 57 | 良 |
2021/1/10 | 周日 | 5 | -1 | 阴~多云 | 西北风 | 2级 | 73 | 良 |
- <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
- None
- 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]
- (730, 1)
- (76, 1)
- X_train after create_dataset (705, 25)
- 0 1 2 ... 22 23 24
- 0 0.162791 0.232558 0.302326 ... 0.255814 0.302326 0.418605
- 1 0.232558 0.302326 0.372093 ... 0.302326 0.418605 0.302326
- 2 0.302326 0.372093 0.255814 ... 0.418605 0.302326 0.302326
- 3 0.372093 0.255814 0.186047 ... 0.302326 0.302326 0.302326
- 4 0.255814 0.186047 0.000000 ... 0.302326 0.302326 0.255814
- .. ... ... ... ... ... ... ...
- 700 0.255814 0.302326 0.302326 ... 0.186047 0.209302 0.232558
- 701 0.302326 0.302326 0.302326 ... 0.209302 0.232558 0.279070
- 702 0.302326 0.302326 0.348837 ... 0.232558 0.279070 0.232558
- 703 0.302326 0.348837 0.372093 ... 0.279070 0.232558 0.209302
- 704 0.348837 0.372093 0.441860 ... 0.232558 0.209302 0.232558
- X_train after reshape (705, 25, 1)
- [[[0.1627907 ]
- [0.23255814]
- [0.30232558]
- ...
- [0.25581395]
- [0.30232558]
- [0.41860465]]
-
- [[0.23255814]
- [0.30232558]
- [0.37209302]
- ...
- [0.30232558]
- [0.41860465]
- [0.30232558]]
-
- [[0.30232558]
- [0.37209302]
- [0.25581395]
- ...
- [0.41860465]
- [0.30232558]
- [0.30232558]]
-
- ...
-
- [[0.30232558]
- [0.30232558]
- [0.34883721]
- ...
- [0.23255814]
- [0.27906977]
- [0.23255814]]
-
- [[0.30232558]
- [0.34883721]
- [0.37209302]
- ...
- [0.27906977]
- [0.23255814]
- [0.20930233]]
-
- [[0.34883721]
- [0.37209302]
- [0.44186047]
- ...
- [0.23255814]
- [0.20930233]
- [0.23255814]]
- Epoch 1/100
- 23/23 [==============================] - 3s 9ms/step - loss: 0.0749
- Epoch 2/100
- 23/23 [==============================] - 0s 9ms/step - loss: 0.0135
- Epoch 3/100
- 23/23 [==============================] - 0s 9ms/step - loss: 0.0100
- Epoch 4/100
- 23/23 [==============================] - 0s 9ms/step - loss: 0.0101
- Epoch 5/100
- 23/23 [==============================] - 0s 9ms/step - loss: 0.0097
- Epoch 6/100
- 23/23 [==============================] - 0s 11ms/step - loss: 0.0091
- Epoch 7/100
- 23/23 [==============================] - 0s 10ms/step - loss: 0.0094
- Epoch 8/100
- 23/23 [==============================] - 0s 11ms/step - loss: 0.0097
- Epoch 9/100
- 23/23 [==============================] - 0s 11ms/step - loss: 0.0089
- Epoch 10/100
- ……
-
- Epoch 97/100
- 23/23 [==============================] - 0s 13ms/step - loss: 0.0059
- Epoch 98/100
- 23/23 [==============================] - 0s 14ms/step - loss: 0.0055
- Epoch 99/100
- 23/23 [==============================] - 0s 13ms/step - loss: 0.0053
- Epoch 100/100
- 23/23 [==============================] - 0s 13ms/step - loss: 0.0053
- 2/2 [==============================] - 1s 4ms/step
- LSTM_val_RMSE: 7.681726490151304
- LSTM_val_MSE: 14.169289610796568
- LSTM_val_R2: 0.5862969525651983
- '''
- 40 0.51478
- 25 0.586529
- 20 0.579569
- 12 0.5689
- 6 0.5397
- '''
-
- LSTM_val_RMSE: 7.666831051805234
- LSTM_val_MSE: 14.324265340141883
- LSTM_val_R2: 0.5817721010539598
-
-
- LSTM_val_RMSE: 7.778038916207498
- LSTM_val_MSE: 15.424474218238425
- LSTM_val_R2: 0.5496491239544908
- LSTM_val_RMSE: 7.7114049938862195
- LSTM_val_MSE: 14.225878442019523
- LSTM_val_R2: 0.5846447192796375
- LSTM_val_RMSE: 7.926438561188859
- LSTM_val_MSE: 18.42118750026751
- LSTM_val_R2: 0.4621536001055656
- LSTM_val_RMSE: 7.950621058540104
- LSTM_val_MSE: 17.132442992213754
- LSTM_val_R2: 0.4997812825788247
- GRU_val_RMSE: 7.801296944756388
- GRU_val_MSE: 15.746488121878885
- GRU_val_R2: 0.5402472317699365
- GRU3_val_RMSE: 7.748423445714951
- GRU3_val_MSE: 15.642613371982792
- GRU3_val_R2: 0.5432800796941399
- GRU3_val_RMSE: 7.767935199334584
- GRU3_val_MSE: 16.609856586892658
- GRU3_val_R2: 0.5150393226336065
- CNN_val_RMSE: 8.157615491942318
- CNN_val_MSE: 26.20883236889009
- CNN_val_R2: 0.23477646949527275
- CNN_val_RMSE: 8.045454928215973
- CNN_val_MSE: 36.41909143818048
- CNN_val_R2: -0.06333412094997337
- (1, 25, 1)
- [[[0.37209302]
- [0.44186047]
- [0.37209302]
- [0.34883721]
- [0.30232558]
- [0.27906977]
- [0.3255814 ]
- [0.23255814]
- [0.34883721]
- [0.3255814 ]
- [0.1627907 ]
- [0.1627907 ]
- [0.27906977]
- [0.34883721]
- [0.3255814 ]
- [0.20930233]
- [0.1627907 ]
- [0.18604651]
- [0.20930233]
- [0.23255814]
- [0.27906977]
- [0.23255814]
- [0.20930233]
- [0.23255814]
- [0.30232558]]]
0 | 1 | 2 | 3 | 4 | |
0 | 0.441860465 | 0.372093023 | 0.348837209 | 0.302325581 | 0.279069767 |
1 | 0.372093023 | 0.348837209 | 0.302325581 | 0.279069767 | 0.325581395 |
2 | 0.348837209 | 0.302325581 | 0.279069767 | 0.325581395 | 0.23255814 |
3 | 0.302325581 | 0.279069767 | 0.325581395 | 0.23255814 | 0.348837209 |
4 | 0.279069767 | 0.325581395 | 0.23255814 | 0.348837209 | 0.325581395 |
5 | 0.325581395 | 0.23255814 | 0.348837209 | 0.325581395 | 0.162790698 |
6 | 0.23255814 | 0.348837209 | 0.325581395 | 0.162790698 | 0.162790698 |
7 | 0.348837209 | 0.325581395 | 0.162790698 | 0.162790698 | 0.279069767 |
8 | 0.325581395 | 0.162790698 | 0.162790698 | 0.279069767 | 0.348837209 |
9 | 0.162790698 | 0.162790698 | 0.279069767 | 0.348837209 | 0.325581395 |
10 | 0.162790698 | 0.279069767 | 0.348837209 | 0.325581395 | 0.209302326 |
11 | 0.279069767 | 0.348837209 | 0.325581395 | 0.209302326 | 0.162790698 |
12 | 0.348837209 | 0.325581395 | 0.209302326 | 0.162790698 | 0.186046512 |
13 | 0.325581395 | 0.209302326 | 0.162790698 | 0.186046512 | 0.209302326 |
14 | 0.209302326 | 0.162790698 | 0.186046512 | 0.209302326 | 0.23255814 |
15 | 0.162790698 | 0.186046512 | 0.209302326 | 0.23255814 | 0.279069767 |
16 | 0.186046512 | 0.209302326 | 0.23255814 | 0.279069767 | 0.23255814 |
17 | 0.209302326 | 0.23255814 | 0.279069767 | 0.23255814 | 0.209302326 |
18 | 0.23255814 | 0.279069767 | 0.23255814 | 0.209302326 | 0.23255814 |
19 | 0.279069767 | 0.23255814 | 0.209302326 | 0.23255814 | 0.302325581 |
20 | 0.23255814 | 0.209302326 | 0.23255814 | 0.302325581 | 0.287259698 |
21 | 0.209302326 | 0.23255814 | 0.302325581 | 0.287259698 | 0.270451009 |
22 | 0.23255814 | 0.302325581 | 0.287259698 | 0.270451009 | 0.272931337 |
23 | 0.302325581 | 0.287259698 | 0.270451009 | 0.272931337 | 0.27097109 |
24 | 0.287259698 | 0.270451009 | 0.272931337 | 0.27097109 | 0.27238524 |
- LSTM
- 未来5天的气温预测值为:
- [[9.352167]
- [8.629394]
- [8.736048]
- [8.651756]
- [8.712565]]
-
-
- CNN
- 未来5天的气温预测值为:
- [[12.766144 ]
- [10.499822 ]
- [ 9.459233 ]
- [ 4.2196836]
- [ 8.652758 ]]
- df_train_val_test_pred df_train_val
- 0 3.306873 4
- 1 5.119420 7
- 2 6.988419 10
- 3 8.891329 13
- 4 5.737236 8
- .. ... ...
- 730 9.352167 0
- 731 8.629394 0
- 732 8.736048 0
- 733 8.651756 0
- 734 8.712565 0
-
- [735 rows x 2 columns]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。