📄 knn_optk.m
字号:
function k = knn_optk(D,d)%KNN_OPTK Optimization of k for the knndd%% k = knn_optk(D,d)%% Optimize the k for the knndd using leave-one-out density% estimation. D is the distance matrix of the original data,% d is the dimensionality of the data.% Copyright: D. Tax, davidt@ph.tn.tudelft.nl% Faculty of Applied Physics, Delft University of Technology% P.O. Box 5046, 2600 GA Delft, The Netherlands% avoid zero distances:smallD = 1e-20;% find the nearest neighbors in the training set:n = size(D,1);D = sort(+D,2);D(:,1) = []; % smallest distance is always 0: so remove that% find the volume of the k-nn hypersphere:logVsph = volsphere(d,1,1);logDr = logVsph + d*log(D)/2;Dr = exp(logDr);% the density becomes:Dr(Dr==0) = smallD;vol = repmat((1:n-1),n,1) ./ Dr;% And the loglikelihood:vol = sum(log(vol),1);% take the maximum:[maxvol,k] = max(vol);%vol = std(log(vol),1)%[maxvol,k] = min(vol);return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -