多维时序 | MATLAB实现GA-BP多变量时间序列预测(遗传算法优化BP神经网络)
目录
- 多维时序 | MATLAB实现GA-BP多变量时间序列预测(遗传算法优化BP神经网络)
- 效果一览
- 基本介绍
- 程序设计
- 参考资料
效果一览
基本介绍
1.MATLAB实现GA-BP多变量时间序列预测(遗传算法优化BP神经网络);
2.运行环境为Matlab2018b;
3.输入多个特征,输出单个变量,考虑历史特征的影响,多变量时间序列预测;
4.data为数据集,MainGABPNTS.m为主程序,运行即可,所有文件放在一个文件夹;
5.命令窗口输出R2、MSE、MAE、MAPE和MBE多指标评价;
程序设计
- 完整程序和数据下载:私信博主回复MATLAB实现GA-BP多变量时间序列预测(遗传算法优化BP神经网络)。
%% 设置优化参数
gen = 50; % 遗传代数
pop_num = 5; % 种群规模
S = size(p_train, 1) * S1 + S1 * size(t_train, 1) + S1 + size(t_train, 1);
% 优化参数个数
bounds = ones(S, 1) * [-1, 1]; % 优化变量边界
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%% 初始化种群
prec = [1e-6, 1]; % epslin 为1e-6, 实数编码
normGeomSelect = 0.09; % 选择函数的参数
arithXover = 2; % 交叉函数的参数
nonUnifMutation = [2 gen 3]; % 变异函数的参数
initPpp = initializega(pop_num, bounds, 'gabpEval', [], prec);
%% 优化算法
[Bestpop, endPop, bPop, trace] = ga(bounds, 'gabpEval', [], initPpp, [prec, 0], 'maxGenTerm', gen,...
'normGeomSelect', normGeomSelect, 'arithXover', arithXover, ...
'nonUnifMutation', nonUnifMutation);
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%% 获取最优参数
[val, W1, B1, W2, B2] = gadecod(Bestpop);
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%% 参数赋值
net.IW{1, 1} = W1;
net.LW{2, 1} = W2;
net.b{1} = B1;
net.b{2} = B2;
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%% 模型训练
net.trainParam.showWindow = 1; % 打开训练窗口
net = train(net, p_train, t_train); % 训练模型
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%% 仿真测试
t_sim1 = sim(net, p_train);
t_sim2 = sim(net, p_test );
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%% 数据反归一化
T_sim1 = mapminmax('reverse', t_sim1, ps_output);
T_sim2 = mapminmax('reverse', t_sim2, ps_output);
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%% 均方根误差
error1 = sqrt(sum((T_sim1 - T_train).^2) ./ M);
error2 = sqrt(sum((T_sim2 - T_test ).^2) ./ N);
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%% 优化迭代曲线
figure
plot(trace(:, 1), 1 ./ trace(:, 2), 'LineWidth', 1.5);
xlabel('迭代次数');
ylabel('适应度值');
string = {'适应度变化曲线'};
title(string)
grid on
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%% 绘图
figure
plot(1: M, T_train, 'y-', 1: M, T_sim1, 'b-', 'LineWidth', 1)
legend('真实值','预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'训练集预测结果对比'; ['RMSE=' num2str(error1)]};
title(string)
xlim([1, M])
grid
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
figure
plot(1: N, T_test, 'm-', 1: N, T_sim2, 'g-', 'LineWidth', 1)
legend('真实值','预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'测试集预测结果对比';['RMSE=' num2str(error2)]};
title(string)
xlim([1, N])
grid
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%% 相关指标计算
% R2
R1 = 1 - norm(T_train - T_sim1)^2 / norm(T_train - mean(T_train))^2;
R2 = 1 - norm(T_test - T_sim2)^2 / norm(T_test - mean(T_test ))^2;
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
disp(['训练集数据的R2为:', num2str(R1)])
disp(['测试集数据的R2为:', num2str(R2)])
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% MAE
mae1 = sum(abs(T_sim1 - T_train)) ./ M ;
mae2 = sum(abs(T_sim2 - T_test )) ./ N ;
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
disp(['训练集数据的MAE为:', num2str(mae1)])
disp(['测试集数据的MAE为:', num2str(mae2)])
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% MBE
mbe1 = sum(T_sim1 - T_train) ./ M ;
mbe2 = sum(T_sim2 - T_test ) ./ N ;
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
disp(['训练集数据的MBE为:', num2str(mbe1)])
disp(['测试集数据的MBE为:', num2str(mbe2)])
————————————————
版权声明:本文为CSDN博主「机器学习之心」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/kjm13182345320/article/details/127993418
参考资料
[1] https://blog.csdn.net/kjm13182345320/article/details/128163536?spm=1001.2014.3001.5502
[2] https://blog.csdn.net/kjm13182345320/article/details/128151206?spm=1001.2014.3001.5502