当前位置:   article > 正文

DL之LSTM/GRU/CNN:基于tensorflow框架分别利用LSTM/GRU、CNN算法对上海最高气温(数据归一化+构造有监督数据集)实现回归预测案例_lstm极端天气预测模型

lstm极端天气预测模型

DL之LSTM/GRU/CNN:基于tensorflow框架分别利用LSTM/GRU、CNN算法对上海最高气温(构造有监督数据集)实现回归预测案例

目录

基于tensorflow框架分别利用LSTM/GRU、CNN算法对上海最高气温(数据归一化+构造有监督数据集)实现回归预测案例

# 1、定义数据集

# 2、特征工程

# 2.1、挑选入模特征

# 2.2、对数据切分并统一进行归一化处理

# 3、模型训练与验证

# 3.1、(构造有监督数据集)切分数据集将训练集和测试集,并转换为LSTM模型所需的数据格式

# 3.2、将数据转换为LSTM/GRU、CNN所需要的3D格式

# 3.3、构建LSTM/GRU、CNN模型

# 3.4、编译并训练模型

# 3.5、模型预测并将预测结果反归一化为原始值

# 3.6、模型评估

# 3.7、绘制预测值对比真实值

LSTM,epochs = 100

LSTM,epochs = 400

LSTM,epochs = 500

GRU2,epochs = 100

GRU2,epochs = 400

GRU3,epochs = 100

GRU3,epochs = 400

CNN,epochs = 100

CNN,epochs = 400

# 4、模型推理

# 4.1、首先需要将最后一个提取出来

# 4.2、指定预测样本个数并基于模型预测

# 对预测结果反归一化

# 4.3、模型预测并反归一化

# 4.4、可视化有预测对比真实值

# 模型预测新数据,并合并为所有预测

# 保存为dataframe格式

# 可视化

LSTM,500


相关文章
DL之LSTM/GRU/CNN:基于tensorflow框架分别利用LSTM/GRU、CNN算法对上海最高气温(数据归一化+构造有监督数据集)实现回归预测案例
DL之LSTM/GRU/CNN:基于tensorflow框架分别利用LSTM/GRU、CNN算法对上海最高气温(数据归一化+构造有监督数据集)实现回归预测案例实现代码

基于tensorflow框架分别利用LSTM/GRU、CNN算法对上海最高气温(数据归一化+构造有监督数据集)实现回归预测案例

# 1、定义数据集

dateweekmax_temperaturemin_temperatureweatherwind_directionwind_levelair_quality_indexair_quality_level
2021/1/1周五4-1晴~多云西北风2级52
2021/1/2周六71晴~多云东北风2级69
2021/1/3周日106东北风2级66
2021/1/4周一137东风2级44
2021/1/5周二82阴~多云东北风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
  1. <class 'pandas.core.frame.DataFrame'>
  2. DatetimeIndex: 805 entries, 2021-01-01 to 2023-03-16
  3. Data columns (total 8 columns):
  4. # Column Non-Null Count Dtype
  5. --- ------ -------------- -----
  6. 0 week 805 non-null object
  7. 1 max_temperature 805 non-null int64
  8. 2 min_temperature 805 non-null int64
  9. 3 weather 805 non-null object
  10. 4 wind_direction 805 non-null object
  11. 5 wind_level 805 non-null object
  12. 6 air_quality_index 667 non-null float64
  13. 7 air_quality_level 775 non-null object
  14. dtypes: float64(1), int64(2), object(5)
  15. memory usage: 56.6+ KB
  16. None
  17. week max_temperature ... air_quality_index air_quality_level
  18. date ...
  19. 2021-01-01 周五 4 ... 52.0
  20. 2021-01-02 周六 7 ... 69.0
  21. 2021-01-03 周日 10 ... 66.0
  22. 2021-01-04 周一 13 ... 44.0
  23. 2021-01-05 周二 8 ... 49.0
  24. ... ... ... ... ... ...
  25. 2023-03-12 周日 12 ... 68.0
  26. 2023-03-13 周一 14 ... 52.0
  27. 2023-03-14 周二 20 ... 55.0
  28. 2023-03-15 周三 23 ... 52.0
  29. 2023-03-16 周四 15 ... 69.0
  30. [805 rows x 8 columns]

# 2、特征工程

# 2.1、挑选入模特征

# 2.2、对数据切分并统一进行归一化处理

  1. (730, 1)
  2. (76, 1)

# 3、模型训练与验证

# 3.1、(构造有监督数据集)切分数据集将训练集和测试集,并转换为LSTM模型所需的数据格式

  1. X_train after create_dataset (705, 25)
  2. 0 1 2 ... 22 23 24
  3. 0 0.162791 0.232558 0.302326 ... 0.255814 0.302326 0.418605
  4. 1 0.232558 0.302326 0.372093 ... 0.302326 0.418605 0.302326
  5. 2 0.302326 0.372093 0.255814 ... 0.418605 0.302326 0.302326
  6. 3 0.372093 0.255814 0.186047 ... 0.302326 0.302326 0.302326
  7. 4 0.255814 0.186047 0.000000 ... 0.302326 0.302326 0.255814
  8. .. ... ... ... ... ... ... ...
  9. 700 0.255814 0.302326 0.302326 ... 0.186047 0.209302 0.232558
  10. 701 0.302326 0.302326 0.302326 ... 0.209302 0.232558 0.279070
  11. 702 0.302326 0.302326 0.348837 ... 0.232558 0.279070 0.232558
  12. 703 0.302326 0.348837 0.372093 ... 0.279070 0.232558 0.209302
  13. 704 0.348837 0.372093 0.441860 ... 0.232558 0.209302 0.232558

# 3.2、将数据转换为LSTM/GRU、CNN所需要的3D格式

  1. X_train after reshape (705, 25, 1)
  2. [[[0.1627907 ]
  3. [0.23255814]
  4. [0.30232558]
  5. ...
  6. [0.25581395]
  7. [0.30232558]
  8. [0.41860465]]
  9. [[0.23255814]
  10. [0.30232558]
  11. [0.37209302]
  12. ...
  13. [0.30232558]
  14. [0.41860465]
  15. [0.30232558]]
  16. [[0.30232558]
  17. [0.37209302]
  18. [0.25581395]
  19. ...
  20. [0.41860465]
  21. [0.30232558]
  22. [0.30232558]]
  23. ...
  24. [[0.30232558]
  25. [0.30232558]
  26. [0.34883721]
  27. ...
  28. [0.23255814]
  29. [0.27906977]
  30. [0.23255814]]
  31. [[0.30232558]
  32. [0.34883721]
  33. [0.37209302]
  34. ...
  35. [0.27906977]
  36. [0.23255814]
  37. [0.20930233]]
  38. [[0.34883721]
  39. [0.37209302]
  40. [0.44186047]
  41. ...
  42. [0.23255814]
  43. [0.20930233]
  44. [0.23255814]]

# 3.3、构建LSTM/GRU、CNN模型

# 3.4、编译并训练模型

  1. Epoch 1/100
  2. 23/23 [==============================] - 3s 9ms/step - loss: 0.0749
  3. Epoch 2/100
  4. 23/23 [==============================] - 0s 9ms/step - loss: 0.0135
  5. Epoch 3/100
  6. 23/23 [==============================] - 0s 9ms/step - loss: 0.0100
  7. Epoch 4/100
  8. 23/23 [==============================] - 0s 9ms/step - loss: 0.0101
  9. Epoch 5/100
  10. 23/23 [==============================] - 0s 9ms/step - loss: 0.0097
  11. Epoch 6/100
  12. 23/23 [==============================] - 0s 11ms/step - loss: 0.0091
  13. Epoch 7/100
  14. 23/23 [==============================] - 0s 10ms/step - loss: 0.0094
  15. Epoch 8/100
  16. 23/23 [==============================] - 0s 11ms/step - loss: 0.0097
  17. Epoch 9/100
  18. 23/23 [==============================] - 0s 11ms/step - loss: 0.0089
  19. Epoch 10/100
  20. ……
  21. Epoch 97/100
  22. 23/23 [==============================] - 0s 13ms/step - loss: 0.0059
  23. Epoch 98/100
  24. 23/23 [==============================] - 0s 14ms/step - loss: 0.0055
  25. Epoch 99/100
  26. 23/23 [==============================] - 0s 13ms/step - loss: 0.0053
  27. Epoch 100/100
  28. 23/23 [==============================] - 0s 13ms/step - loss: 0.0053
  29. 2/2 [==============================] - 1s 4ms/step
  30. LSTM_val_RMSE: 7.681726490151304
  31. LSTM_val_MSE: 14.169289610796568
  32. LSTM_val_R2: 0.5862969525651983

# 3.5、模型预测并将预测结果反归一化为原始值

# 3.6、模型评估

  1. '''
  2. 40 0.51478
  3. 25 0.586529
  4. 20 0.579569
  5. 12 0.5689
  6. 6 0.5397
  7. '''
  8. LSTM_val_RMSE: 7.666831051805234
  9. LSTM_val_MSE: 14.324265340141883
  10. LSTM_val_R2: 0.5817721010539598
  11. LSTM_val_RMSE: 7.778038916207498
  12. LSTM_val_MSE: 15.424474218238425
  13. LSTM_val_R2: 0.5496491239544908

# 3.7、绘制预测值对比真实值

LSTM,epochs = 100

  1. LSTM_val_RMSE: 7.7114049938862195
  2. LSTM_val_MSE: 14.225878442019523
  3. LSTM_val_R2: 0.5846447192796375

LSTM,epochs = 400

  1. LSTM_val_RMSE: 7.926438561188859
  2. LSTM_val_MSE: 18.42118750026751
  3. LSTM_val_R2: 0.4621536001055656

LSTM,epochs = 500

  1. LSTM_val_RMSE: 7.950621058540104
  2. LSTM_val_MSE: 17.132442992213754
  3. LSTM_val_R2: 0.4997812825788247

GRU2,epochs = 100

GRU2,epochs = 400

  1. GRU_val_RMSE: 7.801296944756388
  2. GRU_val_MSE: 15.746488121878885
  3. GRU_val_R2: 0.5402472317699365

GRU3,epochs = 100

  1. GRU3_val_RMSE: 7.748423445714951
  2. GRU3_val_MSE: 15.642613371982792
  3. GRU3_val_R2: 0.5432800796941399

GRU3,epochs = 400

  1. GRU3_val_RMSE: 7.767935199334584
  2. GRU3_val_MSE: 16.609856586892658
  3. GRU3_val_R2: 0.5150393226336065

CNN,epochs = 100

  1. CNN_val_RMSE: 8.157615491942318
  2. CNN_val_MSE: 26.20883236889009
  3. CNN_val_R2: 0.23477646949527275

CNN,epochs = 400

  1. CNN_val_RMSE: 8.045454928215973
  2. CNN_val_MSE: 36.41909143818048
  3. CNN_val_R2: -0.06333412094997337

# 4、模型推理

# 4.1、首先需要将最后一个提取出来

  1. (1, 25, 1)
  2. [[[0.37209302]
  3. [0.44186047]
  4. [0.37209302]
  5. [0.34883721]
  6. [0.30232558]
  7. [0.27906977]
  8. [0.3255814 ]
  9. [0.23255814]
  10. [0.34883721]
  11. [0.3255814 ]
  12. [0.1627907 ]
  13. [0.1627907 ]
  14. [0.27906977]
  15. [0.34883721]
  16. [0.3255814 ]
  17. [0.20930233]
  18. [0.1627907 ]
  19. [0.18604651]
  20. [0.20930233]
  21. [0.23255814]
  22. [0.27906977]
  23. [0.23255814]
  24. [0.20930233]
  25. [0.23255814]
  26. [0.30232558]]]

# 4.2、指定预测样本个数并基于模型预测

01234
00.4418604650.3720930230.3488372090.3023255810.279069767
10.3720930230.3488372090.3023255810.2790697670.325581395
20.3488372090.3023255810.2790697670.3255813950.23255814
30.3023255810.2790697670.3255813950.232558140.348837209
40.2790697670.3255813950.232558140.3488372090.325581395
50.3255813950.232558140.3488372090.3255813950.162790698
60.232558140.3488372090.3255813950.1627906980.162790698
70.3488372090.3255813950.1627906980.1627906980.279069767
80.3255813950.1627906980.1627906980.2790697670.348837209
90.1627906980.1627906980.2790697670.3488372090.325581395
100.1627906980.2790697670.3488372090.3255813950.209302326
110.2790697670.3488372090.3255813950.2093023260.162790698
120.3488372090.3255813950.2093023260.1627906980.186046512
130.3255813950.2093023260.1627906980.1860465120.209302326
140.2093023260.1627906980.1860465120.2093023260.23255814
150.1627906980.1860465120.2093023260.232558140.279069767
160.1860465120.2093023260.232558140.2790697670.23255814
170.2093023260.232558140.2790697670.232558140.209302326
180.232558140.2790697670.232558140.2093023260.23255814
190.2790697670.232558140.2093023260.232558140.302325581
200.232558140.2093023260.232558140.3023255810.287259698
210.2093023260.232558140.3023255810.2872596980.270451009
220.232558140.3023255810.2872596980.2704510090.272931337
230.3023255810.2872596980.2704510090.2729313370.27097109
240.2872596980.2704510090.2729313370.270971090.27238524

# 对预测结果反归一化

  1. LSTM
  2. 未来5天的气温预测值为:
  3. [[9.352167]
  4. [8.629394]
  5. [8.736048]
  6. [8.651756]
  7. [8.712565]]
  8. CNN
  9. 未来5天的气温预测值为:
  10. [[12.766144 ]
  11. [10.499822 ]
  12. [ 9.459233 ]
  13. [ 4.2196836]
  14. [ 8.652758 ]]

# 4.3、模型预测并反归一化

# 4.4、可视化有预测对比真实值

# 模型预测新数据,并合并为所有预测

# 保存为dataframe格式

  1. df_train_val_test_pred df_train_val
  2. 0 3.306873 4
  3. 1 5.119420 7
  4. 2 6.988419 10
  5. 3 8.891329 13
  6. 4 5.737236 8
  7. .. ... ...
  8. 730 9.352167 0
  9. 731 8.629394 0
  10. 732 8.736048 0
  11. 733 8.651756 0
  12. 734 8.712565 0
  13. [735 rows x 2 columns]

# 可视化

LSTM,500

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号