⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chunking.m

📁 支持向量机 这是我的老师编写的matlab源文件 希望对大家有用
💻 M
字号:
function [alpha,bias,svindex]=chunking(X,Y,ker,C)
%function [alpha,bias,svindex]=largesvm(X,Y,ke,C)
%compute the svm for large samples
%alpha   --   optimitional value for supprot vector
%bias    --   b
%svindex --   the index of the suppor vector in whole Samples
%X       --   the input training samples
%Y       --   the expect output corresponding the input samples
%ker     --   the type of kernel function,as follows:
%                  'linear'  -%                  'poly'    - p1 is degree of polynomial%                  'rbf'     - p1 is width of rbfs (sigma)%                  'sigmoid' - p1 is scale, p2 is offset%                  'spline'  -%                  'bspline' - p1 is degree of bspline%                  'fourier' - p1 is degree%                  'erfb'    - p1 is width of rbfs (sigma)%                  'anova'   - p1 is max order of terms
%C      --   factor of penalty. %Author: Weida Zhou   Email:zhouwd@rsp.xidian.edu.cn
if (nargin <2 | nargin>4) % check correct number of arguments    help chunking  else    t0=cputime;    fprintf('Chunking Algorithm Support Vector Classification\n')    fprintf('_____________________________\n')    if (nargin<4) C=Inf;, end    if (nargin<3) ker='linear';, end
epsilon = svtol(C);
%generate the samples
[Samnum,Samcol]=size(X);

%generate the sub-QP
step=10;                 % step samples is added into training every loop
SubQPsize=30;            % number of the Initial sub-samples
selectflag=zeros(Samnum,1);   % flag of samples selection: selected, 1 ; unselected ,0;
selectindex=round(.5+Samnum*rand(SubQPsize,1)); % Index of the selected samples
selectindex=sort(selectindex);
for k=2:1:SubQPsize
   if selectindex(k-1,1)==selectindex(k,1)
      selectindex(k,1)=selectindex(k,1)+1;
   end
end
selectflag(selectindex,1)=1;
SubX=X(selectindex,:);         % Generate the sub-samples
SubY=Y(selectindex,1);

numagainstkkt=2;            % initial number of the samples being against kkt
lastnumagainstkkt=Samnum-SubQPsize; %last number of the samples being against kkt
disp('the number being against kkt condition:');

%Chunking loop until all samples fulfil the kkt conditions
while (numagainstkkt>=1)
   
  [nsv, alpha, bias] =sublargesvc(SubX,SubY,ker,C);

%figure(2);
%clf;
%svcplot(SubX,SubY,ker,alpha,bias);

%Divide the support vector,inlcuding:X--sv,Y--svy,alpha--svalpha
%Build the flag of 1--support vector(margin vector),
%  nonmargin vector(0--alpha=0 or 2--alpha=C).
sv=zeros(0,Samcol);
svy=zeros(0,1);
svalpha=zeros(0,1);
alphaflag=ones(SubQPsize,1);
svindex=[];
for j=1:1:SubQPsize
   if alpha(j,1)<epsilon
      alphaflag(j,1)=0;
   else
       if alpha(j,1)>C-epsilon
          alphaflag(j,1)=2;
          sv=[sv;SubX(j,:)];
          svy=[svy;SubY(j,1)];
          svalpha=[svalpha;alpha(j,1)];
          svindex=[svindex;selectindex(j,1)];
       else       
          sv=[sv;SubX(j,:)];
          svy=[svy;SubY(j,1)];
          svalpha=[svalpha;alpha(j,1)];
          svindex=[svindex;selectindex(j,1)];
       end
    end
    
 end
 %clear SubX,SubY,alphaflag;
 
%generate the index of the unselected samples
[unselectindex,uncol]=find(selectflag==0);
unrow=size(unselectindex,1);
%check the kkt condition 
unselectx=X(unselectindex,:);
unselecty=Y(unselectindex,1);
fx=soutput(sv,svy,svalpha,ker,bias,unselectx);
%clear sv,svy,svalpha,unselectx;
againstkkt=zeros(unrow,1);
numagainstkkt=0;
for k=1:1:unrow
   if  unselecty(k,1)*fx(k,1)<1-epsilon
         againstkkt(k,1)=1-unselecty(k,1)*fx(k,1);
         numagainstkkt=numagainstkkt+1;
   end
end
kkt=[unselectindex,againstkkt];
kkt=sortrows(kkt,2);
%if lastnumagainstkkt==numagainstkkt
   %step=step+1;
  % svindex=selectindex;
%end
lastnumagainstkkt=numagainstkkt;
if (numagainstkkt>=step)
   indexadded=kkt([unrow:-1:unrow-step+1],1);
else
   indexadded=kkt([unrow:-1:unrow-numagainstkkt+1],1);
end

selectindex=[svindex;indexadded];
SubQPsize=size(selectindex,1);
selectindex=sort(selectindex);
selectflag=zeros(Samnum,1);
selectflag(selectindex,1)=1;
SubX=X(selectindex,:);
SubY=Y(selectindex,1);
disp(numagainstkkt);
%pause;
end

alpha=svalpha;
fprintf('Support Vectors : %d (%3.1f%%)\n',size(alpha,1),100*size(alpha,1)/Samnum);fprintf('Execution time: %8.1f seconds\n',cputime - t0);

end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -