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
11x16N  =  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   
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值