📄 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)<littleValue+10^(-3)
continue;
end;
old_alpha2=alpha(j);
new_alpha2=old_alpha2+Y(j)*(E1-E2)/k;
if(new_alpha2>V)
new_alpha2=V;
else
if(new_alpha2<U)
new_alpha2=U;
end
end
alpha(j)=new_alpha2;
alpha(i)=alpha(i)+Y(i)*Y(j)*(old_alpha2-new_alpha2);
% the modification of alphas have been completed
[index value]=find(alpha>littleValue & alpha<C-littleValue);
if(length(index)>=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)<littleValue);
temp=Y(index)'.*(b+(alpha.*Y)'*mat_kernel(:,index));
against_KKT3=sum(temp>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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -