📄 pca_dd.m
字号:
%PCA_DD Principal Component data description%% W = pca_dd(A,fracrej,n)%% Traininig of a PCA, with n features (or explaining a fraction n of% the variance).% Default: n=0.9% Copyright: D. Tax, R.P.W. Duin, davidt@ph.tn.tudelft.nl% Faculty of Applied Physics, Delft University of Technology% P.O. Box 5046, 2600 GA Delft, The Netherlandsfunction W = pca_dd(a,fracrej,n)if (nargin<3) n = 0.9;endif (nargin<2) fracrej = 0.05;endif (nargin<1)|isempty(a) W = mapping(mfilename,{fracrej,n}); returnendif isa(fracrej,'double') %training if ~isa(a,'dataset') %train on training set error('PCA_DD: I need a dataset to train'); end % remove the labels: a = target_class(a); % only use the target class [nlab,lablist,m,k,c,prob,featlist] = dataset(a); % be careful with the mean: meana = ones(m,1)*mean(a); a = +(a - meana); % train it and compute the reconstruction error: if exist('pca','file') w = pca(a, n); else w = klm(a, n); end W = +w; if size(W,2)==k warning('PCA_DD: Output dimensionality is equal to input dimensionality!'); end W(end,:) = []; Proj = W*inv(W'*W)*W'; dif = a - a*Proj; d = sum(dif.*dif,2); % obtain the threshold: thr = -threshold(-d,fracrej); %and save all useful data: % (I know I just have to store W instead of Proj, but I do not like % to compute the inverse of W'*W over and over again, this uses just % some disk and memory space): W.P = Proj; W.m = meana(1,:); W.threshold = thr; W.scale = mean(d); W = mapping(mfilename,W,str2mat('target','outlier'),k,c);else %testing [W,classlist,type,k,c] = mapping(fracrej); % unpack [nlab,lablist,m,k,c,p] = dataset(a); %compute reconstruction error: dif = +a - ones(m,1)*W.m; dif = dif - dif*W.P; out = sum(dif.*dif,2); out = [out, ones(m,1)*W.threshold]; % map to probabilities: newout = dist2dens(out,W.scale); W = dataset(newout,getlab(a),classlist,p,lablist);endreturn
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -