knn_optk.m

来自「数据挖掘的工具箱,最新版的,希望对做这方面研究的人有用」· M 代码 · 共 39 行

M
39
字号
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 + =
减小字号Ctrl + -
显示快捷键?