## [Ai Competition Series] New Crown Epidemic Prediction-Future Numerical Prediction of Stacked LSTM

Participating in the competition for the first time to evaluate the future prediction of the new crown epidemic.
Add new data to the region’s history of new crowns for several months, and then predict the new crowns of the next week.
The historical data given by the official is as follows:

This time the stacked LSTM algorithm is used, which is implemented by Keras

## Stacked LSTM code:

``````import numpy as np
import keras
from keras.layers import *
import matplotlib.pyplot as plt

''' 载入数据集 '''

# 载入数据集 （注：载入数据的函数自行写，这边提供函数实现没有意义）
# 返回list类型，病例的新增数量按日期排列
org_case_count  = get_data.get_xldata(xl_index)

''' 处理数据 '''

# 制作时间序列和标签
def to_supervised(train, n_input, n_out, n_features,step):
data = train
# X存放时间序列，y存放标签
# X的shape是3维，(序列数，序列内元素数，元素的特征值)
# 举例，我代码的X.shape就是(210, 10, 1)，总共有210个序列，每个序列一个元素（连续10天），每个元素一个特征值（新增病例数）
# y的shape是2维,（序列数，标签值）
# 举例，我代码的y.shape就是(210, 1),210个时间序列所推导的标签
X, y = list(), list()
in_start = 0
for _ in range(len(data)):
#in_end:时间序列最后一位；out_end:标签最后一位
in_end = in_start + (n_input-1)*step
out_end =in_end + step*n_out
if out_end < len(data):
tmp_x = list()
for i in range(n_input):
if n_features == 1:
element_x = list()
element_x.append(data[in_start + i * step])
tmp_x.append(element_x)
else:
tmp_x.append(data[in_start+i*step,0:n_features])
X.append(tmp_x)
tmp_y=list()
tmp_y.append(data[in_start + (n_input*step)])
y.append(tmp_y)
in_start += 1
return np.array(X),np.array(y)

# 归一化（除以最大值）
max_val = max(org_case_count)
case_count=list()
for data in org_case_count:
tmp = data/max_val
case_count.append(tmp)
case_count = np.array(case_count)
n_input = 10 # 时间序列元素数量(输入size)，时间序列定义为连续10天；
n_output = 1 # 输出size，我们推导时间序列后的一天；
n_features = 1 # 特征数量
step_lenth = 1 #序列步长
#制作总时间序列
case_data,case_label = to_supervised(case_count,n_input,n_output,n_features,step_lenth)
# 制作训练集数据
train_data = case_data[:-14]
train_label = case_label[:-14]
# 制作验证集数据
valid_data = case_data[-14:-7]
valid_label = case_label[-14:-7]
# 制作测试集数据
test_data =  case_data[-7:]
test_label =  case_label[-7:]

''' 模型训练 '''

# 堆叠LSTM 模型
def encoder_decoder_LSTM_train(n_input,n_out,n_features,epochs_num,train_data,train_label,vaild_data,vaild_label):
model =keras.Sequential()
# 设置堆叠LSTM模型（核心代码）

# 模型编译
print(len(train_data),len(train_label),len(vaild_data),len(vaild_label))
print(train_data.shape,train_label.shape)
model.fit(train_data,train_label,epochs=epochs_num,batch_size=None,shuffle=False,validation_data=(vaild_data,vaild_label))
return model

epochs_num=1000
# 模型开始训练
model = encoder_decoder_LSTM_train(n_input, n_output, n_features, epochs_num,
train_data, train_label, valid_data, valid_label)

''' 模型测试 '''

# 模型开始推理测试集
y_hat = model.predict(test_data).reshape((-1))
# 还原数据
test_label *= max_val
y_hat *= max_val
# 评测公式
s = 0.0
for i in range(len(test_label)):
s += abs(y_hat[i]-test_label[i]) / test_label[i]
print("s=",s)
# 图像展示
fig = plt.figure()
plt.plot(test_label)
plt.plot(y_hat)
plt.show()

``````

final effect:

### Guess you like

Origin blog.csdn.net/sazass/article/details/109682939
Recommended
Ranking
Daily