DL之LSTM/GRU/CNN:基于tensorflow框架分别利用LSTM/GRUCNN算法对上海最高气温实现回归预测案例
Posted 一个处女座的程序猿
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DL之LSTM/GRU/CNN:基于tensorflow框架分别利用LSTM/GRUCNN算法对上海最高气温实现回归预测案例相关的知识,希望对你有一定的参考价值。
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算法对上海最高气温实现回归预测案例实现代码
基于tensorflow框架分别利用LSTM/GRU、CNN算法对上海最高气温实现回归预测案例
# 1、定义数据集
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]
# 2、特征工程
# 2.1、挑选入模特征
# 2.2、对数据切分并统一进行归一化处理
(730, 1)
(76, 1)
# 3、模型训练与验证
# 3.1、切分数据集将训练集和测试集,并转换为LSTM模型所需的数据格式
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
# 3.2、将数据转换为LSTM/GRU、CNN所需要的3D格式
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]]
# 3.3、构建LSTM/GRU、CNN模型
# 3.4、编译并训练模型
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
# 3.5、模型预测并将预测结果反归一化为原始值
# 3.6、模型评估
'''
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
# 3.7、绘制预测值对比真实值
LSTM,epochs = 100
LSTM_val_RMSE: 7.7114049938862195
LSTM_val_MSE: 14.225878442019523
LSTM_val_R2: 0.5846447192796375
LSTM,epochs = 400
LSTM_val_RMSE: 7.926438561188859
LSTM_val_MSE: 18.42118750026751
LSTM_val_R2: 0.4621536001055656
GRU2,epochs = 100
GRU2,epochs = 400
GRU_val_RMSE: 7.801296944756388
GRU_val_MSE: 15.746488121878885
GRU_val_R2: 0.5402472317699365
GRU3,epochs = 100
GRU3_val_RMSE: 7.748423445714951
GRU3_val_MSE: 15.642613371982792
GRU3_val_R2: 0.5432800796941399
GRU3,epochs = 400
GRU3_val_RMSE: 7.767935199334584
GRU3_val_MSE: 16.609856586892658
GRU3_val_R2: 0.5150393226336065
CNN,epochs = 100
CNN_val_RMSE: 8.157615491942318
CNN_val_MSE: 26.20883236889009
CNN_val_R2: 0.23477646949527275
CNN,epochs = 400
CNN_val_RMSE: 8.045454928215973
CNN_val_MSE: 36.41909143818048
CNN_val_R2: -0.06333412094997337
以上是关于DL之LSTM/GRU/CNN:基于tensorflow框架分别利用LSTM/GRUCNN算法对上海最高气温实现回归预测案例的主要内容,如果未能解决你的问题,请参考以下文章
DL之GRU:基于2022年6月最新上证指数数据集结合Pytorch框架利用GRU算法预测最新股票上证指数实现回归预测
DL之Perceptron:感知器(多层感知机/人工神经元)的原理之基于numpy定义2层感知机底层逻辑代码(与门AND/与非门NAND/或门OR是)解决XOR异或问题之详细攻略
基于LNMP的Zabbbix之Zabbix Agent源码详细安装,但不给图
DL:深度学习模型优化之模型训练技巧总结之适时自动调整学习率实现代码