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]);
神经网络机器学习智能算法画图绘图
- 粉丝: 2824
- 资源: 660
最新资源
- python源码教程,超级详细,附开发教程手册,python前端开发,开发学习第四章,入门级
- 整机拆卸自动对位设备工程图机械结构设计图纸和其它技术资料和技术方案非常好100%好用.zip
- 人工智能领域中神经网络的基础概念与应用概述
- Android Studio Ladybug(android-studio-2024.2.1.12-windows-exe.zip.001)
- web前端+HTML+HTML入门+简单的圣诞节主题网页
- xssplayload
- Python实现的学生信息管理系统及其用户界面搭建与安全控制策略
- python源码教程,超级详细,附开发教程手册,python前端开发,入门开发第五章,超级详细
- 谷歌插件chropath-6.1.7-0
- 改良版V免签到-三网免挂支付系统
- 前端教程-JS localstorage的简单应用, 实现简答的数据保存
- python源码教程,超级详细,附开发教程手册,python前端开发,入门学习第六章,教程超详细
- SQL Server数据库恢复工具
- python源码第七章,python源码教程,超级详细,附开发教程手册,python前端开发,开发入门学习第七章
- 【深度学习实战:kaggle自然场景的图像分类-使用keras框架实现vgg16的迁移学习】
- Python期末复习题
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈