1.软件版本
MATLAB2013b
2.本算法理论知识
[1] Lee Y , Lin Y , Wahba G . Multicategory support vector machines. 2001.A05-15
3.部分源码
clc;
clear;
close all;
warning off;
pack;
addpath 'func\'
RandStream.setDefaultStream(RandStream('mt19937ar','seed',8));
%产生测试数据源
%产生测试数据源
Class_Num = 5; %原始数据类别数,>=2 , <= 10
Num = 20; %数据个数
Xt = [];
Yt = [];
Lt = [];
colors{1} = 'bo';
colors{2} = 'r*';
colors{3} = 'gx';
colors{4} = 'k+';
colors{5} = 'ms';
colors{6} = 'c^';
colors{7} = 'y>';
colors{8} = 'b*';
colors{9} = 'rx';
colors{10}= 'ms';
figure;
subplot(131);
for i = 1:Class_Num
%测试数据设置为1维,2维,或者3维,多维测试数据不方便观察
Nums= 10+round(Num*rand(1))+1;
Xo = 3*floor((i+1)/2) + randn(1,Nums);
Yo = 3*mod(i,2) + randn(1,Nums);
Lo = i*ones(1,Nums);
Xt = [Xt,Xo];
Yt = [Yt,Yo];
Lt = [Lt,Lo];
plot(Xo,Yo,colors{1});
hold on;
end
title('原始数据');
Test_Dat = [Xt;Yt];
Category = Lt;
axis square;
Len_xy = axis;
axis([Len_xy(1),Len_xy(2),Len_xy(3),Len_xy(4)]);
subplot(132);
func_MSVM_old(Test_Dat,Category,Class_Num,colors,Len_xy);
%%newmsvm
%%newmsvm
%%newmsvm
%根据MSVM论文的算法进行多分类SVM仿真
%进行训练
Parameter.solver ='Operation';
Parameter.ker ='linear';
Parameter.arg = 1;
Parameter.C = 1;
[dim,num_data] = size(Test_Dat);
CNT = 0;
Category_Index = [];
Classes = zeros(2,(Class_Num-1)*Class_Num/2);
Alpha = zeros(num_data,(Class_Num-1)*Class_Num/2);
b = zeros((Class_Num-1)*Class_Num/2,1);
K = 0;
Test_Dat1 = Test_Dat;
Test_Dat2 = Test_Dat.^2;
Test_Dat3 = [Test_Dat(1,:).*Test_Dat(2,:);Test_Dat(1,:).*Test_Dat(2,:)];
bin_model = [];
Alpha1 = zeros(num_data,(Class_Num-1)*Class_Num/2);
b1 = zeros((Class_Num-1)*Class_Num/2,1);
K1 = 0;
for j1 = 1:Class_Num-1
for j2 = j1+1:Class_Num
CNT = CNT + 1
%dual form
Classes(1,CNT) = j1;
Classes(2,CNT) = j2;
Category_Index1= find(Category==j1);
Category_Index2= find(Category==j2);
Category_Index = unique([Category_Index1,Category_Index2]);
bin_data.X = Test_Dat1(:,Category_Index);
bin_data.y = Category(:,Category_Index);
bin_data.y(find(bin_data.y == j1)) = 1;
bin_data.y(find(bin_data.y == j2)) = 2;
bin_model = feval('Operation',bin_data,Parameter);
%计算alpha
Alpha1(Category_Index(bin_model.POS.inx),CNT) = bin_model.Alpha(:);
%计算b
b1(CNT) = bin_model.b;
%计算K
K1 = K1 + bin_model.K;
end
end
bin_model = [];
CNT = 0;
Category_Index = [];
Alpha2 = zeros(num_data,(Class_Num-1)*Class_Num/2);
b2 = zeros((Class_Num-1)*Class_Num/2,1);
K2 = 0;
for j1 = 1:Class_Num-1
for j2 = j1+1:Class_Num
CNT = CNT + 1
%dual form
Classes(1,CNT) = j1;
Classes(2,CNT) = j2;
Category_Index1= find(Category==j1);
Category_Index2= find(Category==j2);
Category_Index = unique([Category_Index1,Category_Index2]);
bin_data.X = Test_Dat2(:,Category_Index);
bin_data.y = Category(:,Category_Index);
bin_data.y(find(bin_data.y == j1)) = 1;
bin_data.y(find(bin_data.y == j2)) = 2;
bin_model = feval('Operation',bin_data,Parameter);
%计算alpha
Alpha2(Category_Index(bin_model.POS.inx),CNT) = bin_model.Alpha(:);
%计算b
b2(CNT) = bin_model.b;
%计算K
K2 = K2 + bin_model.K;
end
end
bin_model = [];
CNT = 0;
Category_Index = [];
Alpha3 = zeros(num_data,(Class_Num-1)*Class_Num/2);
b3 = zeros((Class_Num-1)*Class_Num/2,1);
K3 = 0;
for j1 = 1:Class_Num-1
for j2 = j1+1:Class_Num
CNT = CNT + 1
%dual form
Classes(1,CNT) = j1;
Classes(2,CNT) = j2;
Category_Index1= find(Category==j1);
Category_Index2= find(Category==j2);
Category_Index = unique([Category_Index1,Category_Index2]);
bin_data.X = Test_Dat3(:,Category_Index);
bin_data.y = Category(:,Category_Index);
bin_data.y(find(bin_data.y == j1)) = 1;
bin_data.y(find(bin_data.y == j2)) = 2;
bin_model = feval('Operation',bin_data,Parameter);
%计算alpha
Alpha3(Category_Index(bin_model.POS.inx),CNT) = bin_model.Alpha(:);
%计算b
b3(CNT) = bin_model.b;
%计算K
K3 = K3 + bin_model.K;
end
end
Alphao{1} = Alpha1;
Alphao{2} = Alpha2;
Alphao{3} = Alpha3;
bo{1} = b1;
bo{2} = b2;
bo{3} = b3;
Ko{1} = K1;
Ko{2} = K2;
Ko{3} = K3;
[V,I] = min([K1(1),K2(1),K3(1)]);
K = Ko{I};
Alpha = Alphao{I};
b = bo{I};
index0 = find(sum(abs(Alpha),2)~= 0);
MSVM_Net.Alpha = Alpha(index0,:);
MSVM_Net.b = b;
MSVM_Net.Classes = Classes;
MSVM_Net.Pos.X = Test_Dat(:,index0);
MSVM_Net.Pos.y = Category(index0);
MSVM_Net.K = K;
MSVM_Net.Parameter = Parameter;
subplot(133);
DIM = size(Test_Dat,1);
for Class_Ind = 1:Class_Num
Index = find(Category == Class_Ind);
if isempty(Index)==0
if DIM == 1
h = plot(Test_Dat(1,Index),zeros(1,length(Index)),colors{Class_Ind});
end
if DIM == 2
h = plot(Test_Dat(1,Index),Test_Dat(2,Index),colors{Class_Ind});
end
if DIM >= 3
h = plot3(Test_Dat(1,Index),Test_Dat(2,Index),Test_Dat(3,Index),colors{Class_Ind});
end
end
hold on;
end
dx = 0.1;
dy = 0.1;
Xgrid = Len_xy(1):dx:Len_xy(2);
Ygrid = Len_xy(3):dy:Len_xy(4);
[X,Y] = meshgrid(Xgrid,Ygrid);
Xmulti = 1;
Ymulti = 1;
for j = 1:DIM
Xmulti = Xmulti*size(X,j);
Ymulti = Ymulti*size(Y,j);
end
View_data = [reshape(X',1,Xmulti);
reshape(Y',1,Ymulti)];
MSVM_ = feval('msvmclassify',View_data,MSVM_Net);
%计算分类错误概率
Ini_Class = Category;
Label_test= msvmclassify(Test_Dat,MSVM_Net);
Label_init= Ini_Class;
Error = length(find((Label_test-Label_init)~=0))/length(Label_test);
Dats = num2str(100*Error);
func_get_boudary(MSVM_,Class_Num,Xgrid,Ygrid);
title(['错误比例:',Dats,'%']);
axis square;
axis([Len_xy(1),Len_xy(2),Len_xy(3),Len_xy(4)]);
clc;
clear;
4.仿真分析
二分类:
三分类:
四分类:
五分类:
六分类:
5.参考文献
[1] Lee Y , Lin Y , Wahba G . Multicategory support vector machines. 2001.A05-15
|