【MATLAB第62期】基于MATLAB的PSO-NN、BBO-NN、前馈神经网络NN回归预测对比
一、数据设置
1、7输入1输出
2、103行样本
3、80个训练样本,23个测试样本
二、效果展示
NN训练集数据的R2为:0.73013
NN测试集数据的R2为:0.23848
NN训练集数据的MAE为:3.0122
NN测试集数据的MAE为:4.4752
NN训练集数据的MAPE为:0.088058
NN测试集数据的MAPE为:0.1302
PSO-NN训练集数据的R2为:0.76673
PSO-NN测试集数据的R2为:0.72916
PSO-NN训练集数据的MAE为:3.124
PSO-NN测试集数据的MAE为:3.1873
PSO-NN训练集数据的MAPE为:0.088208
PSO-NN测试集数据的MAPE为:0.094787
BBO-NN训练集数据的R2为:0.67729
BBO-NN测试集数据的R2为:0.46872
BBO-NN训练集数据的MAE为:3.5204
BBO-NN测试集数据的MAE为:4.4843
BBO-NN训练集数据的MAPE为:0.099475
BBO-NN测试集数据的MAPE为:0.14177
三、代码展示(部分)
%%PSO-NN及BBO-BP回归
%基于生物地理优化进化算法(BBO)
%-----------------------------------------------------------------------
%% 清空环境变量
warning off % 关闭报警信息
close all % 关闭开启的图窗
clear % 清空变量
clc % 清空命令行
rng(0)
%% 导入数据
res = xlsread('数据集.xlsx');%% 划分训练集和测试集
temp = randperm(103);P_train = res(temp(1: 80), 1: 7)';
T_train = res(temp(1: 80), 8)';
M = size(P_train, 2);P_test = res(temp(81: end), 1: 7)';
T_test = res(temp(81: end), 8)';
N = size(P_test, 2);%% 数据归一化
[p_train, ps_input] = mapminmax(P_train, 0, 1);
p_test = mapminmax('apply', P_test, ps_input);[t_train, ps_output] = mapminmax(T_train, 0, 1);
t_test = mapminmax('apply', T_test, ps_output);
%% Learning
n = 9; % Neurons
%----------------------------------------
% 'trainlm' Levenberg-Marquardt
% 'trainbr' Bayesian Regularization (good)
% 'trainrp' Resilient Backpropagation
% 'traincgf' Fletcher-Powell Conjugate Gradient
% 'trainoss' One Step Secant (good)
% 'traingd' Gradient Descent
% Creating the NN ----------------------------
net = feedforwardnet(n,'trainoss');
%---------------------------------------------
% configure the neural network for this dataset
[net tr]= train(net,p_train, t_train);
perf = perform(net,p_train', t_train'); % mse%% 仿真预测
t_sim01=net(p_train);
t_sim02=net(p_test);
T_sim01 = mapminmax('reverse', t_sim01, ps_output);
T_sim02 = mapminmax('reverse', t_sim02, ps_output);%% 均方根误差
error01 = sqrt(sum((T_sim01 - T_train).^2) ./ M);
error02 = sqrt(sum((T_sim02 - T_test ).^2) ./ N);%% 绘图
figure()
subplot(2,1,1)
plot(1: M, T_train, 'r-*', 1: M, T_sim01, 'b-o', 'LineWidth', 1)
legend('真实值', '预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'NN训练集预测结果对比'; ['RMSE=' num2str(error01)]};
title(string)
xlim([1, M])
gridsubplot(2,1,2)
plot(1: N, T_test, 'r-*', 1: N, T_sim02, 'b-o', 'LineWidth', 1)
legend('真实值', '预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'NN测试集预测结果对比'; ['RMSE=' num2str(error02)]};
title(string)
xlim([1, N])
gridt_sim11=net_pso(p_train);
t_sim22=net_pso(p_test);
T_sim11 = mapminmax('reverse', t_sim11, ps_output);
T_sim22 = mapminmax('reverse', t_sim22, ps_output);
%% 均方根误差
error11 = sqrt(sum((T_sim11 - T_train).^2) ./ M);
error22 = sqrt(sum((T_sim22 - T_test ).^2) ./ N);%% 相关指标计算
% R2
R01 = 1 - norm(T_train - T_sim01)^2 / norm(T_train - mean(T_train))^2;
R02 = 1 - norm(T_test - T_sim02)^2 / norm(T_test - mean(T_test ))^2;disp(['NN训练集数据的R2为:', num2str(R01)])
disp(['NN测试集数据的R2为:', num2str(R02)])% MAE
mae01 = sum(abs(T_sim01 - T_train)) ./ M ;
mae02 = sum(abs(T_sim02 - T_test )) ./ N ;disp(['NN训练集数据的MAE为:', num2str(mae01)])
disp(['NN测试集数据的MAE为:', num2str(mae02)])% MAPE mape = mean(abs((YReal - YPred)./YReal));mape01 = mean(abs((T_train - T_sim01)./T_train));
mape02 = mean(abs((T_test - T_sim02 )./T_test)); disp(['NN训练集数据的MAPE为:', num2str(mape01)])
disp(['NN测试集数据的MAPE为:', num2str(mape02)])%% 绘图
figure()
subplot(2,1,1)
plot(1: M, T_train, 'r-*', 1: M, T_sim11, 'b-o', 'LineWidth', 1)
legend('真实值', '预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'PSO-NN训练集预测结果对比'; ['RMSE=' num2str(error11)]};
title(string)
xlim([1, M])
gridsubplot(2,1,2)
plot(1: N, T_test, 'r-*', 1: N, T_sim22, 'b-o', 'LineWidth', 1)
legend('真实值', '预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'PSO-NN测试集预测结果对比'; ['RMSE=' num2str(error22)]};
title(string)
xlim([1, N])
grid%% 相关指标计算
% R2
R11 = 1 - norm(T_train - T_sim11)^2 / norm(T_train - mean(T_train))^2;
R22 = 1 - norm(T_test - T_sim22)^2 / norm(T_test - mean(T_test ))^2;disp(['PSO-NN训练集数据的R2为:', num2str(R11)])
disp(['PSO-NN测试集数据的R2为:', num2str(R22)])% MAE
mae11 = sum(abs(T_sim11 - T_train)) ./ M ;
mae22 = sum(abs(T_sim22 - T_test )) ./ N ;disp(['PSO-NN训练集数据的MAE为:', num2str(mae11)])
disp(['PSO-NN测试集数据的MAE为:', num2str(mae22)])% MAPE mape = mean(abs((YReal - YPred)./YReal));mape11 = mean(abs((T_train - T_sim11)./T_train));
mape22 = mean(abs((T_test - T_sim22 )./T_test)); disp(['PSO-NN训练集数据的MAPE为:', num2str(mape11)])
disp(['PSO-NN测试集数据的MAPE为:', num2str(mape22)])%% BBO优化 NN 权重和偏差
%% PSO优化 NN 权重和偏差
Weights_Bias_bbo=getwb(net_bbo);t_sim31=net_bbo(p_train);
t_sim32=net_bbo(p_test);
T_sim31 = mapminmax('reverse', t_sim31, ps_output);
T_sim32 = mapminmax('reverse', t_sim32, ps_output);
%% 均方根误差
error31 = sqrt(sum((T_sim31 - T_train).^2) ./ M);
error32 = sqrt(sum((T_sim32 - T_test ).^2) ./ N);%% 绘图
figure()
subplot(2,1,1)
plot(1: M, T_train, 'r-*', 1: M, T_sim31, 'b-o', 'LineWidth', 1)
legend('真实值', '预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'BBO-NN训练集预测结果对比'; ['RMSE=' num2str(error31)]};
title(string)
xlim([1, M])
gridsubplot(2,1,2)
plot(1: N, T_test, 'r-*', 1: N, T_sim32, 'b-o', 'LineWidth', 1)
legend('真实值', '预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'BBO-NN测试集预测结果对比'; ['RMSE=' num2str(error32)]};
title(string)
xlim([1, N])
grid%% 相关指标计算
% R2
R31 = 1 - norm(T_train - T_sim31)^2 / norm(T_train - mean(T_train))^2;
R32 = 1 - norm(T_test - T_sim32)^2 / norm(T_test - mean(T_test ))^2;disp(['BBO-NN训练集数据的R2为:', num2str(R31)])
disp(['BBO-NN测试集数据的R2为:', num2str(R32)])% MAE
mae31 = sum(abs(T_sim31 - T_train)) ./ M ;
mae32 = sum(abs(T_sim32 - T_test )) ./ N ;disp(['BBO-NN训练集数据的MAE为:', num2str(mae31)])
disp(['BBO-NN测试集数据的MAE为:', num2str(mae32)])% MAPE mape = mean(abs((YReal - YPred)./YReal));mape31 = mean(abs((T_train - T_sim31)./T_train));
mape32 = mean(abs((T_test - T_sim32 )./T_test)); disp(['BBO-NN训练集数据的MAPE为:', num2str(mape31)])
disp(['BBO-NN测试集数据的MAPE为:', num2str(mape32)])
四、代码获取
后台私信回复“62期”即可获取下载链接。