addpath('/noback/lowbow/translation/svm/helper/'); % --- Generate some toy training values --------------------------------------- numclasses = 2; numclassinst = 25; Mv=100; Nd = numclasses*numclassinst; maxdoclength = 60; theta = normalizeRows(rand(numclasses,Mv))'; X = zeros(Mv,Nd); z = zeros(numclasses,Nd); for ii=1:numclasses for jj=1:numclassinst iter = (ii-1)*numclassinst+jj; z(ii,iter) = 1; %X(:,iter) = multinomial_sample(ceil(maxdoclength*rand),Mv,theta(:,ii),randseed); X(:,iter) = multsamp(ceil(maxdoclength*rand),theta(:,ii)); end end I = randperm(Nd); % not really necessary to randperm z = [-1 1]*z(:,I); % convert cannonical labeling schema for SVM binary classification X = X(:,I); I=find(sum(X,2)==0); Mv=Mv-length(I); X(I,:)=[]; % --- Create and save Kernel -------------------------------------------------- c = 1; t = 0.96; gamma = 1/(2*1^2); T = translation(X,c,t); [ED2 A] = transD2(X,T); K = exp(-gamma * ED2); unit = ones(size(K))/size(K,1); % centering in feature space! K_n = K - unit*K - K*unit + unit*K*unit; numPrinComp = 3; [evecs,evals] = eig(K_n); evals = real(diag(evals)); princomp = evecs(:,1:numPrinComp) * diag(evals(1:numPrinComp).^-0.5); % in the real world, we would make a new kernel with numtest rows and numtrain % cols and premultiply it with princomp % % [ Krr | Kre ] % K = [-----+-----] % [ Ker | Kee ] % % where r denotes training and e denotes testing % Ker would need to be normalized prior to: pX = Ker_n * princomp pX = K_n*princomp; %figure('vis', 'off'); clf; I=find(z==1); plot3(pX(I,1),pX(I,2),pX(I,3),'b.'); hold on; I=find(z==-1); plot3(pX(I,1),pX(I,2),pX(I,3),'r.'); grid on; %saveas(gcf, './kpca', 'png');