%% 第26章 有导师学习神经网络的分类——鸢尾花种类识别
% <html>
% <table border="0" width="600px" id="table1"> <tr> <td><b><font size="2">该案例作者申明:</font></b></td> </tr> <tr><td><span class="comment"><font size="2">1:本人长期驻扎在此<a target="_blank" href="http://www.matlabsky.com/forum-78-1.html"><font color="#0000FF">板块</font></a>里,对该案例提问,做到有问必答。</font></span></td></tr><tr> <td><span class="comment"><font size="2">2</font><font size="2">:此案例有配套的教学视频,视频下载请点击<a href="http://www.matlabsky.com/forum-91-1.html">http://www.matlabsky.com/forum-91-1.html</a></font><font size="2">。 </font></span></td> </tr> <tr> <td><span class="comment"><font size="2"> 3:此案例为原创案例,转载请注明出处(《MATLAB智能算法30个案例分析》)。</font></span></td> </tr> <tr> <td><span class="comment"><font size="2"> 4:若此案例碰巧与您的研究有关联,我们欢迎您提意见,要求等,我们考虑后可以加在案例里。</font></span></td> </tr> <tr> <td><span class="comment"><font size="2"> 5:以下内容为初稿,与实际发行的书籍内容略有出入,请以书籍中的内容为准。</font></span></td> </tr> </table>
% </html>
%% 清空环境变量
clear all
clc
%% 训练集/测试集产生
% 导入数据
load iris_data.mat
% 随机产生训练集和测试集
P_train = [];
T_train = [];
P_test = [];
T_test = [];
for i = 1:3
temp_input = features((i-1)*50+1:i*50,:);
temp_output = classes((i-1)*50+1:i*50,:);
n = randperm(50);
% 训练集——120个样本
P_train = [P_train temp_input(n(1:40),:)'];
T_train = [T_train temp_output(n(1:40),:)'];
% 测试集——30个样本
P_test = [P_test temp_input(n(41:50),:)'];
T_test = [T_test temp_output(n(41:50),:)'];
end
%% 模型建立
result_grnn = [];
result_pnn = [];
time_grnn = [];
time_pnn = [];
for i = 1:4
for j = i:4
p_train = P_train(i:j,:);
p_test = P_test(i:j,:);
%% GRNN创建及仿真测试
t = cputime;
% 创建网络
net_grnn = newgrnn(p_train,T_train);
% 仿真测试
t_sim_grnn = sim(net_grnn,p_test);
T_sim_grnn = round(t_sim_grnn);
t = cputime - t;
time_grnn = [time_grnn t];
result_grnn = [result_grnn T_sim_grnn'];
%% PNN创建及仿真测试
t = cputime;
Tc_train = ind2vec(T_train);
% 创建网络
net_pnn = newpnn(p_train,Tc_train);
% 仿真测试
Tc_test = ind2vec(T_test);
t_sim_pnn = sim(net_pnn,p_test);
T_sim_pnn = vec2ind(t_sim_pnn);
t = cputime - t;
time_pnn = [time_pnn t];
result_pnn = [result_pnn T_sim_pnn'];
end
end
%% 性能评价
% 正确率accuracy
accuracy_grnn = [];
accuracy_pnn = [];
time = [];
for i = 1:10
accuracy_1 = length(find(result_grnn(:,i) == T_test'))/length(T_test);
accuracy_2 = length(find(result_pnn(:,i) == T_test'))/length(T_test);
accuracy_grnn = [accuracy_grnn accuracy_1];
accuracy_pnn = [accuracy_pnn accuracy_2];
end
% 结果对比
result = [T_test' result_grnn result_pnn]
accuracy = [accuracy_grnn;accuracy_pnn]
time = [time_grnn;time_pnn]
%% 绘图
figure(1)
plot(1:30,T_test,'bo',1:30,result_grnn(:,4),'r-*',1:30,result_pnn(:,4),'k:^')
grid on
xlabel('测试集样本编号')
ylabel('测试集样本类别')
string = {'测试集预测结果对比(GRNN vs PNN)';['正确率:' num2str(accuracy_grnn(4)*100) '%(GRNN) vs ' num2str(accuracy_pnn(4)*100) '%(PNN)']};
title(string)
legend('真实值','GRNN预测值','PNN预测值')
figure(2)
plot(1:10,accuracy(1,:),'r-*',1:10,accuracy(2,:),'b:o')
grid on
xlabel('模型编号')
ylabel('测试集正确率')
title('10个模型的测试集正确率对比(GRNN vs PNN)')
legend('GRNN','PNN')
figure(3)
plot(1:10,time(1,:),'r-*',1:10,time(2,:),'b:o')
grid on
xlabel('模型编号')
ylabel('运行时间(s)')
title('10个模型的运行时间对比(GRNN vs PNN)')
legend('GRNN','PNN')
%%
% <html>
% <table width="656" align="left" > <tr><td align="center"><p align="left"><font size="2">相关论坛:</font></p><p align="left"><font size="2">Matlab技术论坛:<a href="http://www.matlabsky.com">www.matlabsky.com</a></font></p><p align="left"><font size="2">M</font><font size="2">atlab函数百科:<a href="http://www.mfun.la">www.mfun.la</a></font></p></td> </tr></table>
% </html>