回归预测 | Matlab实现DE-BP差分算法优化BP神经网络多变量回归预测
目录
- 回归预测 | Matlab实现DE-BP差分算法优化BP神经网络多变量回归预测
- 效果一览
- 基本介绍
- 程序设计
- 参考资料
效果一览
基本介绍
1.Matlab实现DE-BP差分算法优化BP神经网络多变量回归预测(完整源码和数据)
2.运行环境为Matlab2018b;
3.excel数据集,输入多个特征,输出单个变量,多变量回归预测预测,DE_BP.m为主程序,运行即可,所有文件放在一个文件夹;
4.输出优化前后对比图,误差对比图;
代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。
程序设计
- 完整源码和数据获取方式(资源处下载):Matlab实现DE-BP差分算法优化BP神经网络多变量回归预测。
%% 清空环境变量
%选连样本输入输出数据归一化
[inputn,inputps]=mapminmax(input_train);
[outputn,outputps]=mapminmax(output_train);
%构建网络
net=newff(inputn,outputn,hiddennum);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%优化前的BP
net.trainParam.epochs=100;
net.trainParam.lr=0.1;
net.trainParam.goal=0.00001;
[net,~]=train(net,inputn,outputn);
inputn_test=mapminmax('apply',input_test,inputps);
an=sim(net,inputn_test);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 利用差分进化算法选择最佳的BP参数
D=inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum;%变量个数%变量的维数
NP=5; %个体数目
G=30; %最大进化代数%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Xs=1*ones(D,1); %上限
Xx=-1*ones(D,1); %下限
%%%%%%%%%%%%%%%%%%%%%%%%%赋初值%%%%%%%%%%%%%%%%%%%%%%%%
xx=zeros(D,NP); %初始种群
v=zeros(D,NP); %变异种群
u=zeros(D,NP); %选择种群
xchu=rand(D,NP);
for i=1:NP
xx(:,i)=xchu(:,i).*(Xs-Xx)+Xx; %赋初始种群初值
end
%%%%%%%%%%%%%%%%%%%%计算目标函数%%%%%%%%%%%%%%%%%%%%%%%trace(1)=min(Ob);
gbest=100;
%%%%%%%%%%%%%%%%%%%%%%%差分进化循环%%%%%%%%%%%%%%%%%%%%%
for gen=1:G%%%%%%%%%%%%%%%%%%%%%%变异操作%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%r1,r2,r3和m互不相同%%%%%%%%%%%%%%%for m=1:NPr1=randi([1,NP],1,1);while (r1==m)r1=randi([1,NP],1,1);endr2=randi([1,NP],1,1);while (r2==m)||(r2==r1)r2=randi([1,NP],1,1);endr3=randi([1,NP],1,1);while (r3==m)||(r3==r1)||(r3==r2)r3=randi([1,NP],1,1);endv(:,m)=xx(:,r1)+F*(xx(:,r2)-xx(:,r3));end%%%%%%%%%%%%%%%%%%%%%%交叉操作%%%%%%%%%%%%%%%%%%%%%%%r=randi([1,D],1,1);for n=1:Dcr=rand(1);if (cr<=CR)||(n==r)u(n,:)=v(n,:);elseu(n,:)=xx(n,:);endend%%%%%%%%%%%%%%%%%%%边界条件的处理%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%边界吸收%%%%%%%%%%%%%%%%%%%%%%%%%for n=1:Dfor m=1:NPif u(n,m)<Xx(n)u(n,m)=Xx(n);endif u(n,m)>Xs(n)u(n,m)=Xs(n);endendend%%%%%%%%%%%%%%%%%%%%%%选择操作%%%%%%%%%%%%%%%%%%%%%%%for m=1:NPOb1(m)=fitness(u(:,m));endfor m=1:NPif Ob1(m)<Ob(m)xx(:,m)=u(:,m);endendfor m=1:NPOb(m)=fitness(xx(:,m));endfbest=min(Ob);if(fbest<gbest) gbest=fbest;histor(gen)=fbest;elsehistor(gen)=gbest;endend
参考资料
[1] https://blog.csdn.net/kjm13182345320/article/details/129215161
[2] https://blog.csdn.net/kjm13182345320/article/details/128105718