tensorflow2.0,基于LSTM的新冠预测

import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras import layers
df = pd.read_csv('./COVID-19_China.csv')
df.head()
countryEnglishNameconfirmedCountnowconfirmedCountcuredCountdeadCountdeadRatecuredRate
0China54449928173.1250005.147059
1China63959230172.6604074.694836
2China90183936262.8856833.995560
3China1377129739412.9774872.832244
4China2076197149562.6974952.360308

用前三天的新冠数据,预测第四天,构造新的训练数据和标签

value = df['confirmedCount'].values[20:140]
print(len(value))
x = []
y = []
seq = 3
for i in range(len(value)-seq-1):
    x.append(value[i:i+seq])
    y.append(value[i+seq])
118
train_x = (np.array(x[0:90],dtype=np.float32)/100000.).reshape(-1, seq, 1)
train_y = (np.array(y[0:90],dtype=np.float32)/100000.).reshape(-1, 1)
test_x = (np.array(x[90:110],dtype=np.float32)/100000.).reshape(-1, seq, 1)
test_y = (np.array(y[90:110],dtype=np.float32)/100000.).reshape(-1, 1)
print(train_x.shape)
(90, 3, 1)
model = keras.Sequential()
model.add(layers.LSTM(32, input_shape=(train_x.shape[1:]), return_sequences=True))
model.add(layers.LSTM(32, return_sequences=True))
model.add(layers.LSTM(32))
model.add(layers.Dense(1))

model.compile(optimizer=keras.optimizers.Adam(), loss='mae',metrics=['accuracy'])
learning_rate_reduction = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', patience=3, factor=0.5, min_lr=0.00001)
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm (LSTM)                  (None, 3, 32)             4352      
_________________________________________________________________
lstm_1 (LSTM)                (None, 3, 32)             8320      
_________________________________________________________________
lstm_2 (LSTM)                (None, 32)                8320      
_________________________________________________________________
dense (Dense)                (None, 1)                 33        
=================================================================
Total params: 21,025
Trainable params: 21,025
Non-trainable params: 0
_________________________________________________________________
tf.keras.utils.plot_model(model,show_shapes = True)

在这里插入图片描述

history = model.fit(train_x, train_y,
                    batch_size = 128,
                    epochs=1000,
                    validation_data=(test_x, test_y),
                    callbacks=[learning_rate_reduction])
Train on 90 samples, validate on 20 samples
Epoch 1/1000
90/90 [==============================] - 9s 105ms/sample - loss: 0.8074 - accuracy: 0.0000e+00 - val_loss: 0.8302 - val_accuracy: 0.0000e+00


Epoch 1000/1000
90/90 [==============================] - 0s 188us/sample - loss: 0.0089 - accuracy: 0.0000e+00 - val_loss: 0.0085 - val_accuracy: 0.0000e+00
model(train_x).numpy()
array([[0.6455748 ],
       [0.6756083 ],
       [0.725161  ],
       [0.7416195 ],
       [0.7532444 ],
       [0.7631259 ],
       [0.7728118 ],
       [0.7811027 ],
       [0.78719884],
       [0.7901641 ],
       [0.7943473 ],
       [0.7978379 ],
       [0.8003325 ],
       [0.8019932 ],
       [0.8043494 ],
       [0.8063849 ],
       [0.8083892 ],
       [0.81033134],
       [0.8125819 ],
       [0.81462765],
       [0.8154769 ],
       [0.81612027],
       [0.8167974 ],
       [0.8175316 ],
       [0.8181267 ],
       [0.81846243],
       [0.8186874 ],
       [0.81885636],
       [0.81899816],
       [0.81913096],
       [0.8192062 ],
       [0.8193179 ],
       [0.8194875 ],
       [0.8196601 ],
       [0.8198598 ],
       [0.8201023 ],
       [0.8204589 ],
       [0.8208657 ],
       [0.8213866 ],
       [0.8218331 ],
       [0.8224054 ],
       [0.82299423],
       [0.8235421 ],
       [0.82408875],
       [0.8247542 ],
       [0.8253892 ],
       [0.8259906 ],
       [0.8265722 ],
       [0.827014  ],
       [0.8274759 ],
       [0.82790923],
       [0.8282994 ],
       [0.8286484 ],
       [0.82891166],
       [0.82924974],
       [0.82959986],
       [0.82994974],
       [0.8303709 ],
       [0.83072305],
       [0.8310832 ],
       [0.831541  ],
       [0.83207834],
       [0.8324945 ],
       [0.8328698 ],
       [0.8332928 ],
       [0.8340022 ],
       [0.83510035],
       [0.835257  ],
       [0.8354161 ],
       [0.83552617],
       [0.83566976],
       [0.83573765],
       [0.8357913 ],
       [0.83584535],
       [0.83590865],
       [0.8359509 ],
       [0.83600116],
       [0.8360815 ],
       [0.8361014 ],
       [0.8361371 ],
       [0.8361847 ],
       [0.83620495],
       [0.83622694],
       [0.836259  ],
       [0.8362705 ],
       [0.8362912 ],
       [0.83631444],
       [0.8363304 ],
       [0.8363674 ],
       [0.83644485]], dtype=float32)
prediction = list((model(train_x).numpy().reshape(-1))*100000) + list((model(test_x).numpy().reshape(-1))*100000)
plt.plot(value[3:], label='True Value')
plt.plot(prediction[:91], label='LSTM fit')
plt.plot(np.arange(90, 110, 1), prediction[90:], label='LSTM pred')
print(len(value[3:]))
print(len(prediction[90:]))
plt.legend(loc='best')
plt.title('Cumulative infections prediction(China)')
plt.xlabel('Days')
plt.ylabel('Cumulative Cases')
plt.show()
115
20

在这里插入图片描述


  • 8
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值