📄 nndd.m
字号:
%NNDD Nearest neighbour data description method.% % W = nndd(A,fracrej)% % Calculates the Nearest neighbour data description. Training only% consists of the computation of the resemblance of all training% objects to the training data using Leave-one-out.% % See also datasets, mappings, dd_roc% 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,out] = nndd(a,fracrej)if nargin < 2 | isempty(fracrej), fracrej = 0.05; endif nargin < 1 | isempty(a) % empty nndd W = mapping(mfilename,{fracrej}); W = setname(W,'Nearest neighbour data description'); returnendif ~ismapping(fracrej) %training a = +target_class(a); % make sure we have a OneClass dataset [m,k] = size(a); % Compute distance matrix and remove zero distances: distmat = sqeucldistm(a,a); large_D = max(distmat(:)); small_D = 1.0e-10; % almost zero distance distmat = distmat + large_D*(distmat<small_D); %surpress 0 dist. % Apply leave-one-out on the training set: fit = zeros(m,1); for i=1:m D = distmat; [minD minI] = min(D(i,:)); % dist. from z to 1NN in A D(i,minI) = large_D; intdist = min(D(:,minI)); % dist. from 1NN to NN(1NN) fit(i) = minD./intdist; end % Now we can obtain the threshold: thresh = dd_threshold(fit,1-fracrej); % and save all useful data: out = fit; W.x = +a; W.threshold = thresh; W.fit = fit; W.D = min(distmat,[],2); W.scale = mean(fit); W = mapping(mfilename,'trained',W,str2mat('target','outlier'),k,2); W = setname(W,'Nearest neighbour data description');else %testing W = getdata(fracrej); % unpack m = size(a,1); %compute: distmat = sqeucldistm(+a,W.x); %dist between train and test [mindist I] = min(distmat,[],2); % find the closest dist. out = log([mindist./(W.D(I)) repmat(W.threshold,m,1)]); % map to probability newout = dist2dens(out,W.scale); W = setdat(a,newout,fracrej);endreturn
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -