SVM实现一个三类分类问题
2013-04-06 10:01
399 查看
任务要求
用SVM求解一个三类分类问题,实验数据为“鸢尾属植物数据集”,核函数为径向基核函数(RBF),误差评测标准为K折交叉确认误差。
二.实验方案
1. 用quadprog函数实现C-SVC来进行分类#此前在首页部分显示#
——quadprog是matlab中一个求解二次规划的函数,通过适当的参数设置,可以利用quadprog函数实现C-SVC
2. 用matlab自带的SVM工具包来实现分类
——matlab2006版本中集成了SVM工具包,可以通过调用工具包中的svmtrain和svmclassify函数来进行训练和分类
3. 三类问题的分类方法
——将三类问题转化为三个两类问题,分别求出相应的决策函数即可(优点:方法简单易行;缺点:容易形成死区)
三.实验程序
1. 用Quadprog实现
clear all
% Load the data and select features for classification
load fisheriris;
data = meas;
%Get the size of the data
N = size(data,1);
% Extract the Setosa class
groups_temp = ismember(species,'versicolor');%versicolor,virginica,setosa
%convert the group to 1 & -1
groups = 2*groups_temp - ones(N,1);
indices = crossvalind('Kfold', groups);
ErrorMin = 1;
for r=1:1:5
for C=1:1:5
ErrorNum = 0;
for i=1:5
%Use K-fold to get train data and test data
test = (indices == i); train = ~test;
traindata = data(train,:);
traingroup = groups(train,:);
trainlength = length(traingroup);
testdata = data(test,:);
testgroup = groups(test,:);
testlength = length(testgroup);
%Get matrix H of the problem
kfun = [];
for i=1:1:trainlength
for j=1:1:trainlength
%rbf kernel
kfun(i,j)=exp(-1/(r^2)*(traindata(i,:)-traindata(j,:))*(traindata(i,:)-traindata(j,:))');
end
end
%count parameters of quadprog function
H = (traingroup*traingroup').*kfun;
xstart = zeros(trainlength,1);
f = -ones(trainlength,1);
Aeq = traingroup';
beq = 0;
lb = zeros(trainlength,1);
ub = C*ones(trainlength,1);
[alpha,fval] = quadprog(H,f,[],[],Aeq,beq,lb,ub,xstart);
%Get one of the non-zero part of vector alpha to count b
j = 1;
for i=1:size(alpha)
if(alpha(i)>(1e-5))
SvmClass_temper(j,:) = traingroup(i);
SvmAlpha_temper(j,:) = alpha(i);
SvmVector_temper(j,:)= traindata(i,:);
j = j + 1;
tag = i;
end
end
b=traingroup(tag)-(alpha.*traingroup)'*kfun(:,tag);
%Use the function to test the test data
kk = [];
for i=1:testlength
for j=1:trainlength
kk(i,j)=exp(-1/(r^2)*(testdata(i,:)-traindata(j,:))*(testdata(i,:)-traindata(j,:))');
end
end
%then count the function
f=(alpha.*traingroup)'*kk' + b;
for i=1:length(f)
if(f(i)>(1e-5))
f(i)=1;
else
f(i)=-1;
end
end
for i=1:length(f)
if(testgroup(i)~=f(i))
ErrorNum = ErrorNum + 1;
end
end
end
ErrorRate = ErrorNum / N;
if(ErrorRate<ErrorMin)
SvmClass = SvmClass_temper;
SvmAlpha = SvmAlpha_temper;
SvmVector = SvmVector_temper;
ErrorMin = ErrorRate;
用SVM求解一个三类分类问题,实验数据为“鸢尾属植物数据集”,核函数为径向基核函数(RBF),误差评测标准为K折交叉确认误差。
二.实验方案
1. 用quadprog函数实现C-SVC来进行分类#此前在首页部分显示#
——quadprog是matlab中一个求解二次规划的函数,通过适当的参数设置,可以利用quadprog函数实现C-SVC
2. 用matlab自带的SVM工具包来实现分类
——matlab2006版本中集成了SVM工具包,可以通过调用工具包中的svmtrain和svmclassify函数来进行训练和分类
3. 三类问题的分类方法
——将三类问题转化为三个两类问题,分别求出相应的决策函数即可(优点:方法简单易行;缺点:容易形成死区)
三.实验程序
1. 用Quadprog实现
clear all
% Load the data and select features for classification
load fisheriris;
data = meas;
%Get the size of the data
N = size(data,1);
% Extract the Setosa class
groups_temp = ismember(species,'versicolor');%versicolor,virginica,setosa
%convert the group to 1 & -1
groups = 2*groups_temp - ones(N,1);
indices = crossvalind('Kfold', groups);
ErrorMin = 1;
for r=1:1:5
for C=1:1:5
ErrorNum = 0;
for i=1:5
%Use K-fold to get train data and test data
test = (indices == i); train = ~test;
traindata = data(train,:);
traingroup = groups(train,:);
trainlength = length(traingroup);
testdata = data(test,:);
testgroup = groups(test,:);
testlength = length(testgroup);
%Get matrix H of the problem
kfun = [];
for i=1:1:trainlength
for j=1:1:trainlength
%rbf kernel
kfun(i,j)=exp(-1/(r^2)*(traindata(i,:)-traindata(j,:))*(traindata(i,:)-traindata(j,:))');
end
end
%count parameters of quadprog function
H = (traingroup*traingroup').*kfun;
xstart = zeros(trainlength,1);
f = -ones(trainlength,1);
Aeq = traingroup';
beq = 0;
lb = zeros(trainlength,1);
ub = C*ones(trainlength,1);
[alpha,fval] = quadprog(H,f,[],[],Aeq,beq,lb,ub,xstart);
%Get one of the non-zero part of vector alpha to count b
j = 1;
for i=1:size(alpha)
if(alpha(i)>(1e-5))
SvmClass_temper(j,:) = traingroup(i);
SvmAlpha_temper(j,:) = alpha(i);
SvmVector_temper(j,:)= traindata(i,:);
j = j + 1;
tag = i;
end
end
b=traingroup(tag)-(alpha.*traingroup)'*kfun(:,tag);
%Use the function to test the test data
kk = [];
for i=1:testlength
for j=1:trainlength
kk(i,j)=exp(-1/(r^2)*(testdata(i,:)-traindata(j,:))*(testdata(i,:)-traindata(j,:))');
end
end
%then count the function
f=(alpha.*traingroup)'*kk' + b;
for i=1:length(f)
if(f(i)>(1e-5))
f(i)=1;
else
f(i)=-1;
end
end
for i=1:length(f)
if(testgroup(i)~=f(i))
ErrorNum = ErrorNum + 1;
end
end
end
ErrorRate = ErrorNum / N;
if(ErrorRate<ErrorMin)
SvmClass = SvmClass_temper;
SvmAlpha = SvmAlpha_temper;
SvmVector = SvmVector_temper;
ErrorMin = ErrorRate;
相关文章推荐
- 利用SVM实现一个三类分类问题
- SVM实现多分类的程序基础工作(二)——通过一个简单libsvm例子迈入libsvm学习的大门
- Tensorflow实现一个简单的二分类问题
- 朴素贝叶斯算法实现分类问题(三类)matlab代码
- 【ThinkingInJava】16、关于java的接口与抽象类实现的一个问题!
- vc++实现的指针式时钟(赠送问题一个)
- python 实现双纵轴(y)轴图像的绘制(中文label),解决只显示一个折线label的问题
- 如何实现分类的自由改变的问题?
- 对于php实现soap代理的一个常见问题:Class 'SoapClient' not found in PHP的处理方法
- 交叉验证(Cross Validation)方法 && SVM的多分类问题
- 一个操作系统的实现--内存管理中32位和64位的问题
- 用一个form表单实现一次性无限极分类添加功能
- 实现一个算法1/2+1/3+1/4+........Hangover问题
- OpenCV--SVM多分类问题
- !!! STL的string类如何实现CString的Format功能 这是一个经典问题,记住
- 折半查找实现算法二(递归办法)PS:编译后有一个warning,但不影响结果,代码设计上应该还有些问题
- Andrew Ng机器学习笔记+Weka相关算法实现(四)SVM和原始对偶问题
- 在.NET Core中处理一个接口多个不同实现的依赖注入问题
- 一个简单的Java对象池实现——可用来解决SimpleDateFormat的线程安全问题
- IOS中通过给NSString分类实现计算一个字符串中的阿拉伯数字出现的个数