www.pudn.com > svm_G_knn.rar > svm_G_knn.m, change:2017-01-10,size:1906b


%与svmandknn相比,此程序可以实现每类采样点不一致的分类 
 
clc,clear all ; 
t0=clock; 
load train_sample 
load AA 
[m1,n1]=size(train_sample); 
[m2,n2]=size(AA); 
B=cell(1,m2); 
C=cell(1,m2); 
for i=1:1:m2 
    D=find(train_sample(:,1)==AA(i,1)); 
    [m3,n3]=size(D); 
    E=zeros(m3,n1-1); 
    F=zeros(m3,1); 
    for j=1:1:m3  
      n=D(j); 
      E(j,1:end)=train_sample(n,2:end); 
      F(j,1)=train_sample(n,1); 
    end 
    B{1,i}=E; 
    C{1,i}=F; 
end 
q=(m2/2)*(m2-1);           %分类器的总数,m2是训练样本的种类数 
svmstruct=cell(q,1);    %分类模型集 
species=zeros(q,1);      %分类结果集 
%构建分类模型=============================================================== 
p=1; 
for i=1:1:m2 
    for j=(i+1):1:m2 
        data=[B{:,i};B{:,j}]; 
        group=[C{:,i};C{:,j}]; 
        svmstruct{p,1}=svmtrain(data,group); 
        p=p+1; 
    end 
end 
%----------------用分类模型实现分类(在线测量阶段)--------------------------- 
tic 
load text 
%load RR         %AA中存放的是各个区域的中心坐标以及对应的分类标签 
text; 
K=4;             %K的确定需要根据实际情况来定==== 
for r=1:1:q        
species(r,1) = svmclassify(svmstruct{r,1},text);%text的大小是[1,Y-1] 
end 
Probability=zeros(K,4); 
species; 
%选取概率最大的K个区间====================================================== 
A=tabulate(species) ; 
for k=1:1:K ;           %K是设置的阀值,获取K个概率最大的类 
Probability(k,2)=max(max(A(:,3))); 
[x1,y1]=find(A(1:end,3)==max(max(A(:,3)))); 
Probability(k,1)=A(x1,1); 
A(x1,3)=0; 
end 
 
%knn估算位置坐标------------------------------------------------------------ 
for h=1:1:K 
[x2,y2]=find(AA(:,1)==Probability(h,1)); 
Probability(h,3:4)=AA(x2,2:3); 
end 
S=sum(Probability(:,2)); 
Probability(:,2)=Probability(:,2)/S; 
xx=zeros(K,2); 
for g=1:1:K 
    xx(g,1)=Probability(g,2)*Probability(g,3); 
    xx(g,2)=Probability(g,2)*Probability(g,4); 
end 
sum(xx) 
toc 
timecost=etime(clock,t0)