SVM实现一个三类分类问题

任务要求
用SVM求解一个三类分类问题,实验数据为“鸢尾属植物数据集”,核函数为径向基核函数(RBF),误差评测标准为K折交叉确认误差。

二.实验方案
1. 用quadprog函数实现C-SVC来进行分类#此前在首页部分显示#
——quadprog是matlab中一个求解二次规划的函数,通过适当的参数设置,可以利用quadprog函数实现C-SVC
2. 用matlab自带的SVM工具包来实现分类
——matlab2006版本中集成了SVM工具包,可以通过调用工具包中的svmtrain和svmclassify函数来进行训练和分类
3. 三类问题的分类方法
——将三类问题转化为三个两类问题,分别求出相应的决策函数即可(优点:方法简单易行;缺点:容易形成死区)

三.实验程序
1. 用Quadprog实现
11x16 clear all
11x16
%  Load the data and select features  for  classification
11x16load fisheriris;
11x16data 
=  meas;
11x16
% Get the size of the data
11x16
=  size(data, 1 );
11x16
%  Extract the Setosa  class
11x16groups_temp 
=  ismember(species, ' versicolor ' ); % versicolor,virginica,setosa
11x16
% convert the group to  1   &   - 1
11x16groups 
=   2 * groups_temp  -  ones(N, 1 );
11x16
11x16indices 
=  crossvalind( ' Kfold ' , groups);
11x16
11x16ErrorMin 
=   1 ;
11x16
for  r = 1 : 1 : 5
11x16    
for  C = 1 : 1 : 5
11x16        ErrorNum 
=   0 ;        
11x16        
for  i = 1 : 5
11x16            
% Use K - fold to  get  train data and test data
11x16            test 
=  (indices  ==  i); train  =   ~ test;
11x16            
11x16            traindata 
=  data(train,:);
11x16            traingroup 
=  groups(train,:);
11x16            trainlength 
=  length(traingroup);
11x16            
11x16            testdata 
=  data(test,:);
11x16            testgroup 
=  groups(test,:);
11x16            testlength 
=  length(testgroup);
11x16            
11x16            
% Get matrix H of the problem
11x16            kfun 
=  [];
11x16            
for  i = 1 : 1 :trainlength
11x16                
for  j = 1 : 1 :trainlength
11x16                    
% rbf kernel
11x16                    kfun(i,j)
= exp( - 1 / (r ^ 2 ) * (traindata(i,:) - traindata(j,:)) * (traindata(i,:) - traindata(j,:)) ' );
11x16
                end
11x16            end
11x16
11x16            
% count parameters of quadprog function
11x16            H 
=  (traingroup * traingroup ' ).*kfun;
11x16
            xstart  =  zeros(trainlength, 1 );
11x16            f 
=   - ones(trainlength, 1 );
11x16            Aeq 
=  traingroup ' ;
11x16
            beq  =   0 ;
11x16            lb 
=  zeros(trainlength, 1 );
11x16            ub 
=  C * ones(trainlength, 1 );
11x16            
11x16            [alpha,fval] 
=  quadprog(H,f,[],[],Aeq,beq,lb,ub,xstart);
11x16            
11x16            
% Get one of the non - zero part of vector alpha to count b
11x16            j 
=   1 ;
11x16            
for  i = 1 :size(alpha)
11x16                
if (alpha(i) > (1e - 5 ))
11x16                    SvmClass_temper(j,:) 
=  traingroup(i);
11x16                    SvmAlpha_temper(j,:) 
=  alpha(i);
11x16                    SvmVector_temper(j,:)
=  traindata(i,:);
11x16                    j 
=  j  +   1 ;
11x16                    tag 
=  i;
11x16                end
11x16            end
11x16            
11x16            b
= traingroup(tag) - (alpha. * traingroup) ' *kfun(:,tag);
11x16
            
11x16            
% Use the function to test the test data
11x16            kk 
=  [];
11x16            
for  i = 1 :testlength
11x16                
for  j = 1 :trainlength
11x16                    kk(i,j)
= exp( - 1 / (r ^ 2 ) * (testdata(i,:) - traindata(j,:)) * (testdata(i,:) - traindata(j,:)) ' );
11x16
                end
11x16            end
11x16
11x16            
% then count the function
11x16            f
= (alpha. * traingroup) ' *kk '   +  b;           
11x16            
for  i = 1 :length(f)
11x16                
if (f(i) > (1e - 5 ))
11x16                    f(i)
= 1 ;
11x16                
else
11x16                    f(i)
=- 1 ;
11x16                end
11x16            end         
11x16            
11x16            
for  i = 1 :length(f)
11x16                
if (testgroup(i) ~= f(i))
11x16                    ErrorNum 
=  ErrorNum  +   1 ;
11x16                end
11x16            end          
11x16        end
11x16        
11x16        ErrorRate 
=  ErrorNum  /  N;
11x16        
11x16        
if (ErrorRate < ErrorMin)
11x16            SvmClass 
=  SvmClass_temper;
11x16            SvmAlpha 
=  SvmAlpha_temper;
11x16            SvmVector 
=  SvmVector_temper;
11x16            ErrorMin 
=  ErrorRate;
11x16   

你可能感兴趣的:(svm)