📄 kwhiten.m
字号:
%KWHITEN Whiten the data in kernel space.% % W = kwhiten(A,dim,ktype,par1)% % Apply a kernel PCA and retain dim dimensions, or a fraction dim of% the total variance. The data is then rescaled to unit variance in% the feature space. The kernel space is defined by the kernel% function ktype, with the free parameter par1.% % W = kwhiten(A,dim,ktype,par1,reg)% % In some cases the kernel matrix has to be regularized. In that case% the parameter reg has to be filled.% % See also datasets, mappings% Copyright: D. Tax, R.P.W. Duin, duin@ph.tn.tudelft.nl% Faculty of Applied Physics, Delft University of Technology% P.O. Box 5046, 2600 GA Delft, The Netherlands function [W,orglambda] = kwhiten(a,dim,ktype,par1,reg)if nargin < 5, reg = []; endif nargin < 4, par1 = 2; endif nargin < 3, ktype = 'p'; endif nargin < 2 | isempty(dim), dim = 0.95; endif nargin < 1 | isempty(a) error('Sorry, no empty kwithen implemented yet'); returnendif isa(dim,'double') %training if ~isa(a,'dataset') %train on training set error('I need a dataset to train'); end x = target_class(a); % only use the target class [nlab,lablist,m,k,c,prob,featlist] = dataset(x); % train it: wK = proxm(x,ktype,par1); K = +(x*wK); % hm hm, maybe some regularization?: if ~isempty(reg) K = K + eye(m)*reg; end W.Korg = K; % we have to store this for later. K = center(K,K); % find eigenvectors and eigenvalues: [alf,D] = eig(K); lambda = abs(diag(D)); % sort them according to size: [lambda I] = sort(-lambda); lambda = -lambda; alf = alf(:,I); % ok, have eigenvalues larger than 0 or get a specific number of % dimensions: (magic) orglambda = lambda; if (dim<1) I = find(cumsum(lambda)<=(dim*sum(lambda))); alf=alf(:,I); lambda = lambda(I); else I = I(1:dim); alf = alf(:,I); lambda = lambda(I); end% fprintf('%d-D\n',length(I)); % normalize the eigen vectors; alength = sum(alf.*alf,1); % in matlab already normalized a.a=1 ascale = 1./(lambda'.*sqrt(alength)); alf= repmat(ascale,m,1).*alf; %and save all useful data: W.wK = wK; W.alf = alf; W = mapping(mfilename,W,[],k,length(I),1,[],0);else %testing/mapping [nlab,lablist,m,k,c,p,featlist] = dataset(a); [W,classlist,type,k,c] = mapping(dim); % unpack %compute the mapping n = size(W.alf,2); % compute the kernel matrix and center it K = +(a*W.wK); K = center(K,W.Korg); % map the data on the eigenvectors: mapped = zeros(m,n); for i=1:n mp = repmat(W.alf(:,i)',m,1).*K; mapped(:,i) = sum(mp,2); end W = dataset(mapped,getlab(a));endreturn
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -