一.问题描述:
希望根据花朵的四个特种值对花朵的种类进行预测。
数据来源:https://en.wikipedia.org/wiki/Iris_flower_data_set
二.原理描述
首先我们分析我们希望的数据输入输出结构,我们将会有四个输入,分别是鲜花的四个特征最为输入层,和一个输出是鲜花的种类作为输出层,这里我们希望通过计算这个鲜花分别是三种鲜花的概率来判断到底是哪一种鲜花,所以我们可以认为我们的输出层有3个输出。
那么只要我们设计的神经网络符合上面的输入、输出结构都是可以运算出来相应的结果的,我们实验一下一层的情况
识别率在86%左右。
再添加一层试一试
识别率达到了97%。
三.代码:
[f1,f2,f3,f4,class] = textread('trainData.txt' , '%f%f%f%f%f',150);
%特征值归一化
[input,minI,maxI] = premnmx( [f1 , f2 , f3 , f4 ]') ;
%构造输出矩阵
s = length( class) ;
output = zeros( s , 3 ) ;
for i = 1 : s
temp=class( i,1 );
output( i , temp ) = 1 ;
end
%创建神经网络
net = newff( minmax(input) , [10 3] , { 'logsig' 'purelin' } , 'traingdx' ) ;
%设置训练参数
net.trainparam.show = 50 ;
net.trainparam.epochs = 500 ;
net.trainparam.goal = 0.01 ;
net.trainParam.lr = 0.01 ;
%开始训练
net = train( net, input , output' ) ;
%读取测试数据
[t1 t2 t3 t4 c] = textread('testData.txt' , '%f%f%f%f%f',150);
%测试数据归一化
testInput = tramnmx ( [t1,t2,t3,t4]' , minI, maxI ) ;
%仿真
Y = sim( net , testInput )
%统计识别正确率
[s1 , s2] = size( Y ) ;
hitNum = 0 ;
for i = 1 : s2
[m , Index] = max( Y( : , i ) ) ;
if( Index == c(i) )
hitNum = hitNum + 1 ;
end
end
sprintf('识别率是 %3.3f%%',100 * hitNum / s2 )