利用BP神经网络分类iris数据集

来源:互联网 发布:ios app新闻项目源码 编辑:程序博客网 时间:2024/06/06 09:03

clc;

clear;
load('irisdata.mat');
%读取训练数据
%[f1,f2,f3,f4,class] = textread('trainData.txt' , '%f%f%f%f%f',150);
f1=[setosa(:,1);versicolour(:,1);virginica(:,1)];
f2=[setosa(:,2);versicolour(:,2);virginica(:,2)];
f3=[setosa(:,3);versicolour(:,3);virginica(:,3)];
f4=[setosa(:,4);versicolour(:,4);virginica(:,4)];
class=[ones(50,1);ones(50,1)*2;ones(50,1)*3];

%特征值归一化
[input,xmin,xmax] = premnmx( [f1 , f2 , f3 , f4 ]')  ;


%构造输出矩阵
s = length( class) ;
output = zeros( s , 3  ) ;
for i = 1 : s 
   output( i , class( i )  ) = 1 ;
end

% 创建神经网络,5个隐藏神经元,3个输出神经元(3类)

% 隐藏层使用Sigmoid传输函数,输出层采用线性传输函数

% trainFcn属性 'traingdx' 自适应调整学习速率

% learn属性 'learngdm' 附加动量因子的梯度下降学习函数 
net = newff( minmax(input) , [10 3], { 'logsig' 'purelin' } , 'traingdx' ,  'learngdm') ; 

%设置训练参数

net.trainparam.show = 50 ;%每间隔50步显示一次训练结果

net.trainparam.epochs = 500 ;%允许最大训练步数500步

net.trainparam.goal = 0.01 ;%训练目标最小误差0.01

net.trainParam.lr = 0.01 ;%学习速率0.05

%开始训练
net = train( net, input , output' ) ;

%读取测试数据
% [t1 t2 t3 t4 c] = textread('testData.txt' , '%f%f%f%f%f',150);
t1=f1;
t2=f2;
t3=f3;
t4=f4;
c=class;

%测试数据归一化
testInput = tramnmx( [t1,t2,t3,t4]' , amin,xmax) ;


%仿真
Y = sim( net , testInput ) ;

%统计识别正确率,约96%
[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 )


MATLAB源代码及测试数据下载:

http://download.csdn.net/detail/yy_nju/8358027

1 0
原创粉丝点击