基于PaddlePaddle的LSTM神经网络实现中国人口预测
AI达人特训营第二期,依据中国人口逐年变化数据集,使用Paddle搭建LSTM神经网络,实现人口预测。项目流程包括数据预处理、模型构建、模型训练、模型预测、预测结果可视化等。
基于PaddlePaddle的LSTM神经网络实现中国人口预测
1. 引言 ✔️
1.1 项目简介 ✅
本项目属于机器学习范畴,根据指定数据集(中国人口数据集等)使用Paddle框架搭建LSTM神经网络,包括数据预处理、模型构建、模型训练、模型预测、预测结果可视化等。
-
我们将根据中国人口数据集中的多个特征(features),例如:出生人口(万)、中国人均GPA(美元计)、中国性别比例(按照女生=100)、自然增长率(%)等8个特征字段,预测中国未来总人口(万人)这1个标签字段。属于多输入,单输出LSTM神经网路预测范畴。
-
LSTM算法是一种重要的目前使用最多的时间序列算法,是一种特殊的RNN(Recurrent Neural Network,循环神经网络),能够学习长期的依赖关系。循环神经网络的设计初衷之一就是能够捕获长距离输入之间的依赖。而LSTM在RNN基础上引入一个新的内部状态:记忆细胞(memory cell),和三个控制信息传递的逻辑门:输入门(input gate)、遗忘门(forget gate)、输出门(output gate)。
- 记忆细胞(memory cell)与隐状态具有相同的形状(向量维度),其设计目的是用于记录附加的隐藏状态与输入信息,有些文献认为记忆细胞是一种特殊类型的隐状态;
- 输入门(input gate)控制(本时刻)输入观测和(上时刻)隐藏状态中哪些信息会添加进记忆细胞;
- 遗忘门(forget gate)控制忘记上时刻记忆细胞中的哪些内容;
- 输出门(output gate)控制记忆细胞中哪些信息会输出给隐藏状态。
1.2 数据集介绍 ✅
本项目使用的数据集为中国人口预测数据集,包含10个字段,其中8个特征字段,1个标签字段,1个行索引字段,数据集各字段对应的数据类型如下表所示:
- 有些字段为int64类型,需要经过相关的数据处理,才可传入模型进行训练。
- 数据包含50条样本,因此应该合理确定训练数据、测试数据和验证数据
年份 | 出生人口(万) | 总人口(万人) | 中国人均GPA(美元计) | 中国性别比例(按照女生=100) | 自然增长率(%) | 城镇人口(城镇+乡村=100) | 乡村人口 | 美元兑换人民币汇率 | 中国就业人口(万人) |
---|---|---|---|---|---|---|---|---|---|
int64 | int64 | int64 | int64 | float64 | float64 | float64 | float64 | float64 | int64 |
本项目使用环境为:V100;RAM32GB;显存32GB;磁盘100GB;4核CPU;
2. 环境准备
安装模块并导入模块
2.1 安装依赖库
! pip install seaborn -q
2.2 导入所需库
%matplotlib inline
# 导入 paddle
import paddle
import paddle.nn.functional as F
# 导入其他模块
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings("ignore")
print(paddle.__version__)
import paddle.nn as nn
2.4.0
3. 数据处理
3.1 导入数据
population = pd.read_csv("data/data140190/人口.csv")
population.head()
年份 | 出生人口(万) | 总人口(万人) | 中国人均GPA(美元计) | 中国性别比例(按照女生=100) | 自然增长率(%) | 城镇人口(城镇+乡村=100) | 乡村人口 | 美元兑换人民币汇率 | 中国就业人口(万人) | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 1970 | 2710 | 82542 | 113 | 105.90 | 25.95 | 17.38 | 82.62 | 2.4618 | 34432 |
1 | 1971 | 2551 | 84779 | 118 | 105.82 | 23.40 | 17.26 | 82.74 | 2.2673 | 35520 |
2 | 1972 | 2550 | 86727 | 131 | 105.78 | 22.27 | 17.13 | 82.87 | 2.2401 | 35854 |
3 | 1973 | 2447 | 88761 | 157 | 105.86 | 20.99 | 17.20 | 82.80 | 2.0202 | 36652 |
4 | 1974 | 2226 | 90409 | 160 | 105.88 | 17.57 | 17.16 | 82.84 | 1.8397 | 37369 |
3.2 查看各字段类型
因为数据集中有些字段为int64类型,需要转化成float类型,才能在后续导入模型中进行训练,否则会报错。
population.dtypes
年份 int64
出生人口(万) int64
总人口(万人) int64
中国人均GPA(美元计) int64
中国性别比例(按照女生=100) float64
自然增长率(%) float64
城镇人口(城镇+乡村=100) float64
乡村人口 float64
美元兑换人民币汇率 float64
中国就业人口(万人) int64
dtype: object
3.3 数据可视化
3.3.1 特征(features)折线图
绘制出各个特征与年份索引之间的折线图,进行初步观察
因为数据集中包含中文字段,想要能够在绘图中正常显示中文,需要进行如下设定:
from pylab import mpl
from matplotlib.font_manager import FontProperties
myfont=FontProperties(fname=r'/usr/share/fonts/fangzheng/FZSYJW.TTF',size=12)
sns.set(font=myfont.get_name())
from pylab import mpl
from matplotlib.font_manager import FontProperties
myfont=FontProperties(fname=r'/usr/share/fonts/fangzheng/FZSYJW.TTF',size=12)
sns.set(font=myfont.get_name())
titles = [
"出生人口(万)",
"总人口(万人)",
"中国人均GPA(美元计)",
"中国性别比例(按照女生=100)",
"自然增长率(%)",
"城镇人口(城镇+乡村=100)",
"乡村人口",
"美元兑换人民币汇率",
"中国就业人口(万人)",
]
feature_keys = [
"出生人口(万)",
"总人口(万人)",
"中国人均GPA(美元计)",
"中国性别比例(按照女生=100)",
"自然增长率(%)",
"城镇人口(城镇+乡村=100)",
"乡村人口",
"美元兑换人民币汇率",
"中国就业人口(万人)",
]
colors = [
"blue",
"chocolate",
"green",
"red",
"purple",
"brown",
"darkblue",
"black",
"magenta",
]
date_time_key = "年份"
def show_raw_visualization(data):
time_data = data[date_time_key]
fig, axes = plt.subplots(
nrows=3, ncols=3, figsize=(15, 15), dpi=100, facecolor="w", edgecolor="k"
)
for i in range(len(feature_keys)):
key = feature_keys[i]
c = colors[i % (len(colors))]
t_data = data[key]
t_data.index = time_data
t_data.head()
ax = t_data.plot(
ax=axes[i // 3, i % 3],
color=c,
title="{}".format(titles[i], key),
rot=25,
)
ax.legend([titles[i]])
plt.tight_layout()
show_raw_visualization(population)
3.3.2 箱型图
查看部分数据的分布情况,下面抽取了出生人口(万)、总人口(万人)、中国人均GPA(美元计)、中国就业人口(万人)这四个字段进行箱型图展示。
from pylab import mpl
from matplotlib.font_manager import FontProperties
myfont=FontProperties(fname=r'/usr/share/fonts/fangzheng/FZSYJW.TTF',size=12)
sns.set(font=myfont.get_name())
plt.figure(figsize=(15,8),dpi=100)
plt.subplot(1,4,1)
sns.boxplot(y="出生人口(万)", data=population, saturation=0.9)
plt.subplot(1,4,2)
sns.boxplot(y="总人口(万人)", data=population, saturation=0.9)
plt.subplot(1,4,3)
sns.boxplot(y="中国人均GPA(美元计)", data=population, saturation=0.9)
plt.subplot(1,4,4)
sns.boxplot(y="中国就业人口(万人)", data=population, saturation=0.9)
plt.tight_layout()
3.3.3 相关性分析
查看变量两两之间的相关性
- 分析特征之间的相关性:可以考虑将两个相关性较强的特征选择一个进行保留,因为本数据集特征字段不是很多,就不考虑剔除了。
- 分析目标值与特征之间的相关性:发现目标值与特征间(包括年份)的相关性都比较强,因此年份也可以作为特征传入模型进行训练。
corr = population.corr()
# 调用热力图绘制相关性关系
plt.figure(figsize=(10,10),dpi=100)
sns.heatmap(corr, square=True, linewidths=0.1, annot=True)
<matplotlib.axes._subplots.AxesSubplot at 0x7f1396a65150>
3.4 数据处理
3.4.1 重复值检测
查看特征中是否包含重复值,返回false说明没有重复值。无需剔除。
population.duplicated().any()
3.4.2 缺失值检测
查看是否有缺失的样本。返回True说明无缺失值,无需进行额外处理。
pd.notnull(population).all()
3.4.3 转换字段类型
我们需要将int类型字段转化为float类型的字段。
- 首先使用如下语句查看数据集各个字段类型。
- 接下来我们将int64转为float64并替换原数据字段
population.dtypes
population['出生人口(万)'] = population['出生人口(万)'].astype('float64')
population['总人口(万人)'] = population['总人口(万人)'].astype('float64')
population['中国人均GPA(美元计)'] = population['中国人均GPA(美元计)'].astype('float64')
population['中国就业人口(万人)'] = population['中国就业人口(万人)'].astype('float64')
4. 数据预处理
4.1 构造窗口
- 对于LSTM网络,此数据集中,“总人口(万人)”为目标值
- 在每个窗口后的“总人口(万人)”数据为此窗口的目标值
- 目标值为表中的第3列数据
data[ , 2]
# 窗口划分
def split_windows(data, size):
X = []
Y = []
# X作为数据,Y作为标签
# 滑动窗口,步长为1,构造窗口化数据,每一个窗口的数据标签是窗口末端的“总人口(万人)”
for i in range(len(data) - size):
X.append(data[i:i+size, :])
Y.append(data[i+size, 2])
return np.array(X), np.array(Y)
4.2 划分数据集
- 划分训练集与测试集样本
- 切分比例为 0.6 : 0.4 0.6 :0.4 0.6:0.4
- 对划分后的数据集进行可视化
all_data = population.values
split_fraction = 0.6
train_split = int(split_fraction * int(population.shape[0])) # 计算切分的训练集大小
train_data = all_data[:train_split, :]
test_data = all_data[train_split:, :]
plt.figure(figsize=(10, 5))
# 数据可视化
plt.plot(np.arange(train_data.shape[0]), train_data[:, 2], label='train data')
plt.plot(np.arange(train_data.shape[0], train_data.shape[0] + test_data.shape[0]), test_data[:, 2], label='test data')
plt.legend()
<matplotlib.legend.Legend at 0x7f515ce32ad0>
4.3 归一化并划分窗口
- 使用最大最小归一化对训练数据与测试数据进行归一化
- 使用之前定义的窗口划分函数对训练集和测试集的特征与目标值进行划分
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaled_train_data = scaler.fit_transform(train_data)
# 使用训练集的最值对测试集归一化,保证训练集和测试集的分布一致性
scaled_test_data = scaler.transform(test_data)
# 训练集测试集划分
window_size = 5
train_X, train_Y = split_windows(scaled_train_data, size=window_size)
test_X, test_Y = split_windows(scaled_test_data, size=window_size)
print('train shape', train_X.shape, train_Y.shape)
print('test shape', test_X.shape, test_Y.shape)
train shape (25, 5, 10) (25,)
test shape (15, 5, 10) (15,)
5. 模型搭建
5.1 搭建LSTM模型
- Reshape层:用于将输入数据转换为指定的输入形式。
- 2D Conv层:进行卷积操作,滤波器个数为64,padding设置为same用于获取相同大小的feature map,激活函数为relu。
- Maxpooling层:进行下采样,然后接一个Dropout用于防止过拟合。
- LSTM层:定义了一层LSTM层
- Linear:最后设置一层全连接层进行输出下一时刻的预测值
- 窗口大小:window_size = 5
- 特征数:fea_num = 10
- 批处理大小:batch_size = 5
# 输入的指标维度
window_size = 5
fea_num = 10
batch_size = 5
class CNN_LSTM(paddle.nn.Layer):
def __init__(self, window_size, fea_num):
super().__init__()
self.window_size = window_size
self.fea_num = fea_num
self.conv1 = paddle.nn.Conv2D(in_channels=1, out_channels=64, stride=1, kernel_size=3, padding='same')
self.relu1 = paddle.nn.ReLU()
self.pool = paddle.nn.MaxPool2D(kernel_size=2, stride=1, padding='same')
self.dropout = paddle.nn.Dropout2D(0.3)
self.lstm1 = paddle.nn.LSTM(input_size=64*fea_num, hidden_size=128, num_layers=1, time_major=False)
# self.lstm2 = paddle.nn.LSTM(input_size=128, hidden_size=64, num_layers=1, time_major=False)
self.fc = paddle.nn.Linear(in_features=128, out_features=64)
self.relu2 = paddle.nn.ReLU()
self.head = paddle.nn.Linear(in_features=64, out_features=1)
def forward(self, x):
x = x.reshape([x.shape[0], 1, self.window_size, self.fea_num])
x = self.conv1(x)
x = self.relu1(x)
x = self.pool(x)
x = self.dropout(x)
x = x.reshape([x.shape[0], self.window_size, -1])
x, (h, c) = self.lstm1(x)
# x, (h,c) = self.lstm2(x)
x = x[:,-1,:] # 最后一个LSTM只要窗口中最后一个特征的输出
x = self.fc(x)
x = self.relu2(x)
x = self.head(x)
return x
5.2 查看搭建的网络参数
# 打印网络结构
model = CNN_LSTM(window_size, fea_num)
paddle.summary(model, (5, 5, 10)) # batchsize样本数,窗口大小,特征数量
------------------------------------------------------------------------------------------------
Layer (type) Input Shape Output Shape Param #
================================================================================================
Conv2D-6 [[5, 1, 5, 10]] [5, 64, 5, 10] 640
ReLU-11 [[5, 64, 5, 10]] [5, 64, 5, 10] 0
MaxPool2D-6 [[5, 64, 5, 10]] [5, 64, 5, 10] 0
Dropout2D-6 [[5, 64, 5, 10]] [5, 64, 5, 10] 0
LSTM-6 [[5, 5, 640]] [[5, 5, 128], [[1, 5, 128], [1, 5, 128]]] 394,240
Linear-11 [[5, 128]] [5, 64] 8,256
ReLU-12 [[5, 64]] [5, 64] 0
Linear-12 [[5, 64]] [5, 1] 65
================================================================================================
Total params: 403,201
Trainable params: 403,201
Non-trainable params: 0
------------------------------------------------------------------------------------------------
Input size (MB): 0.00
Forward/backward pass size (MB): 0.53
Params size (MB): 1.54
Estimated Total Size (MB): 2.07
------------------------------------------------------------------------------------------------
{'total_params': 403201, 'trainable_params': 403201}
5.3 定义超参数与优化器
# 定义超参数
base_lr = 0.005
BATCH_SIZE = 5
EPOCH = 50
lr_schedual = paddle.optimizer.lr.CosineAnnealingDecay(learning_rate=base_lr, T_max=EPOCH, verbose=True)
loss_fn = nn.MSELoss()
metric = paddle.metric.Accuracy()
opt = paddle.optimizer.Adam(parameters=model.parameters(), learning_rate=lr_schedual, beta1=0.9, beta2=0.999)
Epoch 0: CosineAnnealingDecay set learning rate to 0.005.
def process(data, bs):
l = len(data)
tmp = []
for i in range(0, l, bs):
if i + bs > l:
tmp.append(data[i:].tolist())
else:
tmp.append(data[i:i+bs].tolist())
tmp = np.array(tmp)
return tmp
# 处理训练数据
train_X = process(train_X, 5)
train_Y = process(train_Y, 5)
print(train_X.shape, train_Y.shape)
(5, 5, 5, 10) (5, 5)
6. 模型训练
- 在训练过程中打印训练轮数、训练损失
- 保存模型参数到路径
work/cnn_lstm_ep50_lr0.005
- 训练超参数已在前面指定:
- base_lr = 0.005
- BATCH_SIZE = 5
- EPOCH = 50
# 模型训练
for epoch in range(EPOCH):
model.train()
loss_train = 0
for batch_id, data in enumerate(train_X):
label = train_Y[batch_id]
data = paddle.to_tensor(data, dtype='float32')
label = paddle.to_tensor(label, dtype='float32')
label = label.reshape([label.shape[0],1])
y = model(data)
loss = loss_fn(y, label)
opt.clear_grad()
loss.backward()
opt.step()
loss_train += loss.item()
print("[TRAIN]epoch: {}, loss: {:.4f}".format(epoch+1, loss_train))
lr_schedual.step()
# 保存模型参数
paddle.save(model.state_dict(), 'work/cnn_lstm_ep50_lr0.005.params')
paddle.save(lr_schedual.state_dict(), 'work/cnn_lstm_ep50_lr0.005.pdopts')
[TRAIN]epoch: 1, loss: 0.0401
Epoch 1: CosineAnnealingDecay set learning rate to 0.004995066821070679.
[TRAIN]epoch: 2, loss: 0.1485
Epoch 2: CosineAnnealingDecay set learning rate to 0.004980286753286194.
[TRAIN]epoch: 3, loss: 0.0396
Epoch 3: CosineAnnealingDecay set learning rate to 0.004955718126821721.
[TRAIN]epoch: 4, loss: 0.0177
Epoch 4: CosineAnnealingDecay set learning rate to 0.004921457902821577.
[TRAIN]epoch: 5, loss: 0.0053
Epoch 5: CosineAnnealingDecay set learning rate to 0.004877641290737884.
[TRAIN]epoch: 6, loss: 0.0173
Epoch 6: CosineAnnealingDecay set learning rate to 0.004824441214720628.
[TRAIN]epoch: 7, loss: 0.0139
Epoch 7: CosineAnnealingDecay set learning rate to 0.004762067631165049.
[TRAIN]epoch: 8, loss: 0.0050
Epoch 8: CosineAnnealingDecay set learning rate to 0.0046907667001096585.
[TRAIN]epoch: 9, loss: 0.0060
Epoch 9: CosineAnnealingDecay set learning rate to 0.004610819813755038.
[TRAIN]epoch: 10, loss: 0.0104
Epoch 10: CosineAnnealingDecay set learning rate to 0.0045225424859373685.
[TRAIN]epoch: 11, loss: 0.0175
Epoch 11: CosineAnnealingDecay set learning rate to 0.004426283106939473.
[TRAIN]epoch: 12, loss: 0.0116
Epoch 12: CosineAnnealingDecay set learning rate to 0.004322421568553529.
[TRAIN]epoch: 13, loss: 0.0025
Epoch 13: CosineAnnealingDecay set learning rate to 0.004211367764821722.
[TRAIN]epoch: 14, loss: 0.0037
Epoch 14: CosineAnnealingDecay set learning rate to 0.004093559974371725.
[TRAIN]epoch: 15, loss: 0.0014
Epoch 15: CosineAnnealingDecay set learning rate to 0.003969463130731184.
[TRAIN]epoch: 16, loss: 0.0032
Epoch 16: CosineAnnealingDecay set learning rate to 0.003839566987447492.
[TRAIN]epoch: 17, loss: 0.0011
Epoch 17: CosineAnnealingDecay set learning rate to 0.0037043841852542887.
[TRAIN]epoch: 18, loss: 0.0035
Epoch 18: CosineAnnealingDecay set learning rate to 0.0035644482289126822.
[TRAIN]epoch: 19, loss: 0.0029
Epoch 19: CosineAnnealingDecay set learning rate to 0.0034203113817116958.
[TRAIN]epoch: 20, loss: 0.0013
Epoch 20: CosineAnnealingDecay set learning rate to 0.003272542485937369.
[TRAIN]epoch: 21, loss: 0.0013
Epoch 21: CosineAnnealingDecay set learning rate to 0.003121724717912137.
[TRAIN]epoch: 22, loss: 0.0020
Epoch 22: CosineAnnealingDecay set learning rate to 0.0029684532864643126.
[TRAIN]epoch: 23, loss: 0.0011
Epoch 23: CosineAnnealingDecay set learning rate to 0.002813333083910762.
[TRAIN]epoch: 24, loss: 0.0013
Epoch 24: CosineAnnealingDecay set learning rate to 0.0026569762988232844.
[TRAIN]epoch: 25, loss: 0.0011
Epoch 25: CosineAnnealingDecay set learning rate to 0.002500000000000001.
[TRAIN]epoch: 26, loss: 0.0017
Epoch 26: CosineAnnealingDecay set learning rate to 0.0023430237011767175.
[TRAIN]epoch: 27, loss: 0.0010
Epoch 27: CosineAnnealingDecay set learning rate to 0.0021866669160892404.
[TRAIN]epoch: 28, loss: 0.0022
Epoch 28: CosineAnnealingDecay set learning rate to 0.0020315467135356893.
[TRAIN]epoch: 29, loss: 0.0007
Epoch 29: CosineAnnealingDecay set learning rate to 0.0018782752820878637.
[TRAIN]epoch: 30, loss: 0.0005
Epoch 30: CosineAnnealingDecay set learning rate to 0.0017274575140626331.
[TRAIN]epoch: 31, loss: 0.0011
Epoch 31: CosineAnnealingDecay set learning rate to 0.0015796886182883067.
[TRAIN]epoch: 32, loss: 0.0010
Epoch 32: CosineAnnealingDecay set learning rate to 0.0014355517710873192.
[TRAIN]epoch: 33, loss: 0.0010
Epoch 33: CosineAnnealingDecay set learning rate to 0.001295615814745712.
[TRAIN]epoch: 34, loss: 0.0006
Epoch 34: CosineAnnealingDecay set learning rate to 0.0011604330125525083.
[TRAIN]epoch: 35, loss: 0.0005
Epoch 35: CosineAnnealingDecay set learning rate to 0.0010305368692688178.
[TRAIN]epoch: 36, loss: 0.0010
Epoch 36: CosineAnnealingDecay set learning rate to 0.000906440025628276.
[TRAIN]epoch: 37, loss: 0.0005
Epoch 37: CosineAnnealingDecay set learning rate to 0.0007886322351782785.
[TRAIN]epoch: 38, loss: 0.0005
Epoch 38: CosineAnnealingDecay set learning rate to 0.0006775784314464719.
[TRAIN]epoch: 39, loss: 0.0008
Epoch 39: CosineAnnealingDecay set learning rate to 0.0005737168930605274.
[TRAIN]epoch: 40, loss: 0.0010
Epoch 40: CosineAnnealingDecay set learning rate to 0.0004774575140626318.
[TRAIN]epoch: 41, loss: 0.0006
Epoch 41: CosineAnnealingDecay set learning rate to 0.000389180186244963.
[TRAIN]epoch: 42, loss: 0.0040
Epoch 42: CosineAnnealingDecay set learning rate to 0.00030923329989034114.
[TRAIN]epoch: 43, loss: 0.0016
Epoch 43: CosineAnnealingDecay set learning rate to 0.0002379323688349517.
[TRAIN]epoch: 44, loss: 0.0006
Epoch 44: CosineAnnealingDecay set learning rate to 0.0001755587852793717.
[TRAIN]epoch: 45, loss: 0.0006
Epoch 45: CosineAnnealingDecay set learning rate to 0.00012235870926211623.
[TRAIN]epoch: 46, loss: 0.0018
Epoch 46: CosineAnnealingDecay set learning rate to 7.854209717842261e-05.
[TRAIN]epoch: 47, loss: 0.0012
Epoch 47: CosineAnnealingDecay set learning rate to 4.428187317827821e-05.
[TRAIN]epoch: 48, loss: 0.0004
Epoch 48: CosineAnnealingDecay set learning rate to 1.9713246713805593e-05.
[TRAIN]epoch: 49, loss: 0.0003
Epoch 49: CosineAnnealingDecay set learning rate to 4.933178929321104e-06.
[TRAIN]epoch: 50, loss: 0.0013
Epoch 50: CosineAnnealingDecay set learning rate to 0.0.
7. 模型预测
- 使用训练好的模型对测试集数据进行预测
- 因为之前测试集数据经过了归一化,因此我们还需要对预测结果以及测试集真实数据进行反归一化
- 可视化展示测试集的预测标签数据与真实标签数据
# 加载模型
model = CNN_LSTM(window_size, fea_num)
model_dict = paddle.load('work/cnn_lstm_ep150_lr0.005.params')
model.load_dict(model_dict)
test_X = paddle.to_tensor(test_X, dtype='float32')
prediction = model(test_X)
prediction = prediction.cpu().numpy()
prediction = prediction.reshape(prediction.shape[0], )
# 反归一化
scaled_prediction = prediction * (scaler.data_max_[2] - scaler.data_min_[2]) + scaler.data_min_[2]
scaled_true = test_Y * (scaler.data_max_[2] - scaler.data_min_[2]) + scaler.data_min_[2]
# 画图
plt.plot(range(len(scaled_true)), scaled_true, label='true')
plt.plot(range(len(scaled_prediction)), scaled_prediction, label='prediction', marker='*')
plt.legend()
RMSE 11441.667016788204
8. 模型评估
我们使用均方根误差RMSE作为预测精度的评估标准
均方根误差计算方法如下所示:
from sklearn.metrics import mean_squared_error
8. 模型评估
我们使用均方根误差RMSE作为预测精度的评估标准
均方根误差计算方法如下所示:
[外链图片转存中...(img-8WK114wR-1671074814488)]
```python
from sklearn.metrics import mean_squared_error
print('RMSE', np.sqrt(mean_squared_error(scaled_prediction, scaled_true)))
RMSE 11441.667016788204
9. 总结与展望 🎯
- 本项目使用Paddle搭建LSTM神经网络模型进行了完整预测流程。
- 由于本数据集样本量有限,今后可以选择在样本更充分的数据集进行实验。
- 另外,在今后可以考虑改善网络结构与参数来进一步优化模型。
再一次感谢项目导师
顾茜
的指导
本项目成员均来自
北京科技大学
飞桨领航团
请点击此处查看本环境基本用法.
Please click here for more detailed instructions.
此文章为搬运
原项目链接
更多推荐
所有评论(0)