www.pudn.com > kernelSVM.rar > SVC_Fun.m


function outPutPar=SVC_TwoClass(class_one,class_two,sizeTrainClassOne,sizeTrainClassTwo,kPar,C,kType) 
% the function for generating the two_class based classification 
% InputPar: 
% class_one: the first class data; 
% class_two: the second class data; 
% sizeTrainClassOne: the size of the training number of the first class 
% data; 
% sizeTrainClassTwo: the size of the training number of the second class 
% data; 
% OutputPar: 
% outPutPar.w:  
% outPutPar.b; 
% outPutPar.alpha; 
% Written by WangZhe on 2005-04-05; 
 
Y=[ones(sizeTrainClassOne,1);-1*ones(sizeTrainClassTwo,1)]; 
% input data completed 
 
X=[class_one(1:sizeTrainClassOne,:);class_two(1:sizeTrainClassTwo,:)]; 
 
mat_kernel=Kernel(X,X,kType,kPar); 
% calculate the kernel use a kernel 
 
trainSampleNum=size(X,1); 
alpha=zeros(trainSampleNum,1); 
b=0; 
% set the value of alpha ,b and C 
 
littleValue=0; 
% the little value control the precison of this program 
againstKKTAllowed=0; 
% the allowed times that is against KKT conditions 
loop_times=0; 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
 
while(loop_times<5000) 
     
    % Step1:Choosing two alphas of samples 
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
    index=randperm(trainSampleNum); 
    i=index(1); j=index(2); 
    % Here using the technique of randomly selecting i,j 
     
    if(Y(i)~=Y(j)) 
        U=max(0,alpha(j)-alpha(i)); 
        V=min(C,C-alpha(i)+alpha(j)); 
    else 
        U=max(0,alpha(i)+alpha(j)-C); 
        V=min(C,alpha(i)+alpha(j)); 
    end 
    % Compute the value of U and V 
     
    E1=(alpha.*Y)'*mat_kernel(:,i)+b-Y(i); 
    E2=(alpha.*Y)'*mat_kernel(:,j)+b-Y(j); 
     
    k=mat_kernel(i,i)+mat_kernel(j,j)-2*mat_kernel(i,j); 
    if abs(k)V) 
        new_alpha2=V; 
    else 
        if(new_alpha2littleValue & alpha=1) 
        b_vector=Y(index)-((alpha.*Y)'*mat_kernel(:,index))'; 
        b=sum(b_vector)/length(index); 
    end 
    % reset the value of b 
     
    % Step2: check KKT condition 
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
    temp=Y(index)'.*(b+(alpha.*Y)'*mat_kernel(:,index)); 
    against_KKT2=sum(abs(temp-1)>littleValue); 
     
    [index value]=find(abs(alpha-C)1+littleValue); 
     
    [index value]=find(alpha<=littleValue); 
    temp=Y(index)'.*(b+(alpha.*Y)'*mat_kernel(:,index)); 
    against_KKT1=sum(temp<1-littleValue); 
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
     
    loop_times=loop_times+1; 
     
    if(against_KKT1+against_KKT2+against_KKT3 <= againstKKTAllowed) 
        break; 
    end     
end 
 
numberSV=length(find(abs(alpha)>littleValue)); 
outPutPar.w=((alpha.*Y)'*X)'; 
outPutPar.b=b; 
outPutPar.alpha=alpha;