clc
clear
close all
warning off
rng('default')
load maydata.mat
m1=5;
m =720;
rng(12)
n=randperm(800);
%节点个数
inputnum=5;
hiddennum=10;
outputnum=1;
m = round(0.9*length(num));
n = randperm(length(num));
%训练数据和预测数据
input_train=num(n(1:m),1:m1)';
input_test=num(n(m+1:end),1:m1)';
output_train=num(n(1:m),m1+1)';
output_test=num(n(m+1:end),m1+1)';
%选连样本输入输出数据归一化
[inputn,inputps]=mapminmax(input_train);
[outputn,outputps]=mapminmax(output_train);
%构建网络
% net=newff(inputn,outputn,hiddennum);
net = newff(minmax(inputn),[10,1],{'logsig','tansig'},'trainlm');
dim=inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum;
maxgen=30; % 进化次数
sizepop=20; %种群规模
popmax=5;
popmin=-5;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
P_percent = 0.2; % The population size of producers accounts for "P_percent" percent of the total population size
pNum = round( sizepop * P_percent ); % The population size of the producers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:sizepop
pop(i,:)=5*rands(1,dim);
% V(i,:)=rands(1,21);
fitness(i)=fun(pop(i,:),inputnum,hiddennum,outputnum,net,inputn,outputn);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pFit = fitness;
[ fMin, bestI ] = min( fitness ); % fMin denotes the global optimum fitness value
bestX = pop( bestI, : ); % bestX denotes the global optimum position corresponding to fMin
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%麻雀搜索算法 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for t = 1 : maxgen
[ ans, sortIndex ] = sort( pFit );% Sort.
[fmax,B]=max( pFit );
worse= pop(B,:);
r2=rand(1);
if(r2<0.8)
for i = 1 : pNum % Equation (3)
r1=rand(1);
pop( sortIndex( i ), : ) = pop( sortIndex( i ), : )*exp(-(i)/(r1*maxgen));
fitness(sortIndex( i ))=fun(pop(sortIndex( i ),:),inputnum,hiddennum,outputnum,net,inputn,outputn);
end
else
for i = 1 : pNum
pop( sortIndex( i ), : ) = pop( sortIndex( i ), : )+randn(1)*ones(1,dim);
fitness(sortIndex( i ))=fun(pop(sortIndex( i ),:),inputnum,hiddennum,outputnum,net,inputn,outputn);
end
end
[ fMMin, bestII ] = min( fitness );
bestXX = pop( bestII, : );
for i = ( pNum + 1 ) : sizepop % Equation (4)
A=floor(rand(1,dim)*2)*2-1;
if( i>(sizepop/2))
pop( sortIndex(i ), : )=randn(1)*exp((worse-pop( sortIndex( i ), : ))/(i)^2);
else
pop( sortIndex( i ), : )=bestXX+(abs(( pop( sortIndex( i ), : )-bestXX)))*(A'*(A*A')^(-1))*ones(1,dim);
end
fitness(sortIndex( i ))=fun(pop(sortIndex( i ),:),inputnum,hiddennum,outputnum,net,inputn,outputn);
end
c=randperm(numel(sortIndex));
b=sortIndex(c(1:3));
for j = 1 : length(b) % Equation (5)
if( pFit( sortIndex( b(j) ) )>(fMin) )
pop( sortIndex( b(j) ), : )=bestX+(randn(1,dim)).*(abs(( pop( sortIndex( b(j) ), : ) -bestX)));
else
pop( sortIndex( b(j) ), : ) =pop( sortIndex( b(j) ), : )+(2*rand(1)-1)*(abs(pop( sortIndex( b(j) ), : )-worse))/ ( pFit( sortIndex( b(j) ) )-fmax+1e-50);
end
fitness(sortIndex( i ))=fun(pop(sortIndex( i ),:),inputnum,hiddennum,outputnum,net,inputn,outputn);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1 : sizepop
if ( fitness( i ) < pFit( i ) )
pFit( i ) = fitness( i );
pop(i,:) = pop(i,:);
end
if( pFit( i ) < fMin )
fMin= pFit( i );
bestX =pop( i, : );
end
end
yy(t)=fMin;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 迭代寻优
x=bestX
%% 结果分析
plot(yy)
title(['适应度曲线 ' '终止代数=' num2str(maxgen)]);
xlabel('进化代数');ylabel('适应度');
ylim([min(yy)*0.8 max(yy)*1.2])
%% 把最优初始阀值权值赋予网络预测
% %用麻雀搜索算法优化的BP网络进行值预测
w1=x(1:inputnum*hiddennum);
B1=x(inputnum*hiddennum+1:inputnum*hiddennum+hiddennum);
w2=x(inputnum*hiddennum+hiddennum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum);
B2=x(inputnum*hiddennum+hiddennum+hiddennum*outputnum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum);
net.iw{1,1}=reshape(w1,hiddennum,inputnum);
net.lw{2,1}=reshape(w2,outputnum,hiddennum);
net.b{1}=reshape(B1,hiddennum,1);
net.b{2}=B2;
%% 训练
%网络进化参数
net.trainParam.epochs=5000;
net.trainParam.lr=0.1;
net.trainParam.goal=0.00001;
%网络训练
[net,tr]=train(net,inputn,outputn);
%%预测
%数据归一化
inputn_test=mapminmax('apply',input_test,inputps);
input_test1=[20000 471 63 1205 1677]';
inputn_test1=mapminmax('apply',input_test1,inputps);
an=sim(net,inputn_test);
an1=sim(net,inputn_test1);
test_simu=round(mapminmax('reverse',an,outputps));
test_simu1=round(mapminmax('reverse',an1,outputps));
error=test_simu-output_test;
figure
plot(test_simu,'r-o')
hold on
plot(output_test,'k-*')
hold off
xlabel('样本')
ylim([0.5 5.5])
set(gca,'fontsize',12)
legend('ssa-bp','实际值')
[output_test1,my] = sort(output_test);
test_simu1 = test_simu(my);
figure
plot(test_simu1(1,:),'r-o')%预测的结果数据画图:代表虚线,O代表圆圈标识,G代表绿色
hold on
plot(output_test1(1,:),'k-*');%期望数据,即真实的数据画图,-代表实现,*就是代表*的标识
hold on
legend('SSA-BP预测输出','期望输出')%标签
title('BP神经网络','fontsize',12)%标题 字体大小为12
xlabel('样本')
ylim([0.5 5.5])
set(gca,'fontsize',12)
figure
plot(error)
title('仿真预测误差','fontsize',12);
xlabel('','fontsize',12);
ylabel('误差','fontsize',12);
acc = sum(output_test==test_simu)/length(output_test)
[cfmat,order] = confusionmat(output_test,test_simu);
mat=cfmat;
k=5;
%% 混淆矩阵制图
[cfmat,order] = confusionmat(output_test,test_simu);
mat=cfmat;
k=5;
figure
xx = [ones(256,1) ones(256,1) [255:-100/255: 155 ]'/255];
imagesc(mat); %# Create a colored plot of the matrix values
colormap(xx); %flipud(summer) # Change the colormap to gray (so higher values are
title('SSA-BP混淆矩阵');
textStrings = num2str(mat(:),'%0.02f'); %# Create strings from the matrix values
textStrings = strtrim(cellstr(textStrings)); %# Remove any space padding
[x,y] = meshgrid(1:k);
hStrings=text(x(:),y(:),textStrings(:),'HorizontalAlignment','center');
midValue = mean(get(gca,'CLim')); %# Get the middle value of the color range
textColors = repmat(mat(:) > midValue,1,3);
%set(hStrings,{'Color'},num2cell(textColors,2)); %# Change the text colors锛?
set(gca,'XTick',1:5,...
'XTickLabel',{'1','2','3','4','5'},... %# and tick labels
'YTick',1:5,... %鍚屼笂
'YTickLabel',{'1','2','3','4','5'},...
'TickLength',[0 0]);
没有合适的资源?快使用搜索试试~ 我知道了~
基于麻雀算法优化BP神经网络的多分类识别,基于麻雀算法改进BP神经网络的多分类预测代码,ssa-bp多分类代码
共9个文件
jpg:5个
m:3个
mat:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 3 下载量 179 浏览量
2023-08-04
23:48:11
上传
评论 2
收藏 89KB ZIP 举报
温馨提示
基于MATLAB编程,用麻雀算法优化BP神经网络的权值阈值,并用的改进的BP神经网络和标准的神经网络分别进行多分类训练测试,然后对比效果,证明改进的效果更好,代码齐全,数据完整,可以运行,代码有注释,方便扩展到其他数据
资源推荐
资源详情
资源评论
收起资源包目录
ssa-bp.zip (9个子文件)
SSA.m 7KB
w.jpg 33KB
1.jpg 15KB
5.jpg 17KB
maydata.mat 10KB
fun.m 1KB
bpp.m 1KB
3.jpg 18KB
4.jpg 14KB
共 9 条
- 1
资源评论
- 2401_835900622024-06-05有没有教一下怎么运行啊?
- m0_597247972024-05-24总算找到了想要的资源,搞定遇到的大问题,赞赞赞!
- boo.3842024-03-07这个资源内容超赞,对我来说很有价值,很实用,感谢大佬分享~
神经网络机器学习智能算法画图绘图
- 粉丝: 2798
- 资源: 659
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功