登录后复制
clc;clear;close all;warning off;pack;addpath 'func\'RandStream.setDefaultStream(RandStream('mt19937ar','seed',8));%产生测试数据源%产生测试数据源Class_Num = 5; %原始数据类别数,>=2 , <= 10Num = 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;endtitle('原始数据'); 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; endendbin_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; endendbin_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; endendAlphao{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;enddx = 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;1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.80.81.82.83.84.85.86.87.88.89.90.91.92.93.94.95.96.97.98.99.100.101.102.103.104.105.106.107.108.109.110.111.112.113.114.115.116.117.118.119.120.121.122.123.124.125.126.127.128.129.130.131.132.133.134.135.136.137.138.139.140.141.142.143.144.145.146.147.148.149.150.151.152.153.154.155.156.157.158.159.160.161.162.163.164.165.166.167.168.169.170.171.172.173.174.175.176.177.178.179.180.181.182.183.184.185.186.187.188.189.190.191.192.193.194.195.196.197.198.199.200.201.202.203.204.205.206.207.208.209.210.211.212.213.214.215.216.217.218.219.220.221.222.223.224.225.226.227.228.229.230.231.
二分类:
三分类:
四分类:
五分类:
六分类:
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删