[Home]

##### RampTWSVC

A Matlab code for Ramp-based twin support vector clustering. (You could Right-Click [Codes] , and Save, then you can download the results.)

##### Reference

Zhen Wang, et.al. Ramp-based Twin Support Vector Clustering. submitted.

##### Main Function

function Y= RampTWSVC(X,iY,c1,c2) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % RampTWSVC: main function % % Y= RampTWSVC(X,iY,c1,c2) % % Input: % X: Data matrix. (nolinear version is obtained by K(X,X)) % iY: First initial labels of X (can be given randomly or by NN-graph). % then, it would be updated in iteration. % Parameters - c1,c2. The fields in options that can be set: % c1,c2: (0,inf) Paramter to tune the weight. % % Output: % Y: The prediction of X in this iteration. % Examples: % X=rand(50,10); % iY = randint(50,1,[1,5]); % c1=1;c2=1; % Y= RampTWSVC(X,iY,c1,c2) % Reference: % Zhen Wang, Yuan-Hai Shao, Nai-Yang Deng, "Ramp-based Twin support vector clustering" Submitted. % % Version 1.0 --Nov/2018 % % Written by Zhen Wang (wangz11@mails.jlu.edu.cn) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% mu=0; Y= iY; som=1; ite=0; delta=0.3; s=-0.2; num=max(iY); m=size(X,1); while som>1e-3 && ite<100 ite=ite+1; totalw=[]; totalb=[]; for i=1:num if ~isempty(find(iY==i, 1)) tY=iY; tY(tY==i)=-999; tY(tY>0)=-1; tY(tY==-999)=1; [w,b]=Trainone(X,tY,c1,c2,mu,delta,s); if norm(w)>0 totalw=[totalw,w]; totalb=[totalb,b]; end end end n=size(totalw,2); val=zeros(m,n); for i=1:m for j=1:n val(i,j)=abs(X(i,:)*totalw(:,j)+totalb(1,j)); end end if ~isempty(totalw) [~,pY]=min(val,[],2); else pY=ones(size(X,1),1); end som=norm(pY-Y,1); Y=pY; end end function [w,b]= Trainone(X,Y,c1,c2,mu,delta,s) %Train a classifier, using training set to compute alpha [m,n]=size(X); m1=length(find(Y==1)); m2=m-m1; X1=X(Y==1,:); X2=X(Y~=1,:); MAXITE=100; ite=0; tol=1e-2; %%%% Initialization %%%% [w,b] = SVM(X,Y,c1); tmp=X1*w+b; d1=zeros(m1,1); d1(tmp2+delta-s,1)=-1; tmp=X2*w+b; d2=zeros(m2,1); d2(tmp-s,1)=-1; w_old=ones(n,1); %%%%%%%%%%%%%% if mu<=0 X11=X1*X1'; X12=X1*X2'; X22=X2*X2'; else ; end H=[X11,-X11,X12,-X12; -X11,X11,-X12,X12; X12',-X12',X22,-X22; -X12',X12',-X22,X22]; Aeq=[ones(1,m1),-ones(1,m1),ones(1,m2),-ones(1,m2)]; %Lb=zeros(2*m,1); Ub=[c1*ones(2*m1,1);c2*ones(2*m2,1)]; while norm(w-w_old)>tol && itetol if i<=2*m1 && alpha(i,1)2*m1 && alpha(i,1)2+delta-s,1)=-1; tmp=X2*w+b; d2=zeros(m2,1); d2(tmp-s,1)=-1; end end function [w,b] = SVM(X,Y,C) %SVM solver by SMO m=size(X,1); e=ones(m,1); a= SMO(X*X',e,Y,0,C*e); w=0; for i=1:m if a(i)~=0 w=w+a(i)*Y(i)*X(i,:)'; end end b=0; for i=1:m if a(i)>0 && a(i)0); %The index of y>0 if delta>sum(C) disp('Error: delta>sum(C)'); return; else tmp=0; j=1; for i=1:m tmp=tmp+C(i,1); if j>length(posind) break; end if tmp<=delta a(posind(j),1)=C(i,1); j=j+1; else a(posind(j),1)=delta-sum(a); break; end end end ma=-inf; i1=-1; Ma=inf; i2=-1; for i=1:m if (a(i,1)0) || (a(i,1)>0 && y(i,1)<0) tmp=y(i,1)*(f(i,1)-H(i,:)*a); if tmp>ma ma=tmp; i1=i; end elseif (a(i,1)0 && y(i,1)>0) tmp=y(i,1)*(f(i,1)-H(i,:)*a); if tmptol && t0 && y(i2,1)>0) || (y(i1,1)<0 && y(i2,1)<0) U=max([0,y(i1,1)*Dt-C(i1,1)]); V=min([C(i2,1),y(i1,1)*Dt]); else U=max([0,-y(i1,1)*Dt]); V=min([C(i2,1),C(i1,1)-y(i1,1)*Dt]); end if a(i2,1)>V a(i2,1)=V; elseif a(i2,1) < U a(i2,1)=U; end a(i1,1)=y(i1,1)*Dt-y(i1,1)*y(i2,1)*a(i2,1); ma=-inf; i1=-1; Ma=inf; i2=-1; for i=1:m if (a(i,1)0) || (a(i,1)>0 && y(i,1)<0) tmp=y(i,1)*(f(i,1)-H(i,:)*a); if tmp>ma ma=tmp; i1=i; end elseif (a(i,1) < C(i,1) &&y(i,1)<0) || (a(i,1)>0 && y(i,1)>0) tmp=y(i,1)*(f(i,1)-H(i,:)*a); if tmp < Ma Ma=tmp; i2=i; end end end end end