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()
countryEnglishName | confirmedCount | nowconfirmedCount | curedCount | deadCount | deadRate | curedRate | |
---|---|---|---|---|---|---|---|
0 | China | 544 | 499 | 28 | 17 | 3.125000 | 5.147059 |
1 | China | 639 | 592 | 30 | 17 | 2.660407 | 4.694836 |
2 | China | 901 | 839 | 36 | 26 | 2.885683 | 3.995560 |
3 | China | 1377 | 1297 | 39 | 41 | 2.977487 | 2.832244 |
4 | China | 2076 | 1971 | 49 | 56 | 2.697495 | 2.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