资源简介
基于密度峰值快速搜索发现聚类中心的聚类算法源代码。
代码片段和文件信息
clear all
close all
disp(‘The only input needed is a distance matrix file‘)
disp(‘The format of this file should be: ‘)
disp(‘Column 1: id of element i‘)
disp(‘Column 2: id of element j‘)
disp(‘Column 3: dist(ij)‘)
mdist=input(‘name of the distance matrix file\n‘‘s‘);
disp(‘Reading input distance matrix‘)
xx=load(mdist);
ND=max(xx(:2));
NL=max(xx(:1));
if (NL>ND)
ND=NL;
end
N=size(xx1);
for i=1:ND
for j=1:ND
dist(ij)=0;
end
end
for i=1:N
ii=xx(i1);
jj=xx(i2);
dist(iijj)=xx(i3);
dist(jjii)=xx(i3);
end
percent=2.0;
fprintf(‘average percentage of neighbours (hard coded): %5.6f\n‘ percent);
position=round(N*percent/100);
sda=sort(xx(:3));
dc=sda(position);
fprintf(‘Computing Rho with gaussian kernel of radius: %12.6f\n‘ dc);
for i=1:ND
rho(i)=0.;
end
%
% Gaussian kernel
%
for i=1:ND-1
for j=i+1:ND
rho(i)=rho(i)+exp(-(dist(ij)/dc)*(dist(ij)/dc));
rho(j)=rho(j)+exp(-(dist(ij)/dc)*(dist(ij)/dc));
end
end
%
% “Cut off“ kernel
%
%for i=1:ND-1
% for j=i+1:ND
% if (dist(ij) % rho(i)=rho(i)+1.;
% rho(j)=rho(j)+1.;
% end
% end
%end
maxd=max(max(dist));
[rho_sortedordrho]=sort(rho‘descend‘);
delta(ordrho(1))=-1.;
nneigh(ordrho(1))=0;
for ii=2:ND
delta(ordrho(ii))=maxd;
for jj=1:ii-1
if(dist(ordrho(ii)ordrho(jj)) delta(ordrho(ii))=dist(ordrho(ii)ordrho(jj));
nneigh(ordrho(ii))=ordrho(jj);
end
end
end
delta(ordrho(1))=max(delta(:));
disp(‘Generated file:DECISION GRAPH‘)
disp(‘column 1:Density‘)
disp(‘column 2:Delta‘)
fid = fopen(‘DECISION_GRAPH‘ ‘w‘);
for i=1:ND
fprintf(fid ‘%6.2f %6.2f\n‘ rho(i)delta(i));
end
disp(‘Select a rectangle enclosing cluster centers‘)
scrsz = get(0‘ScreenSize‘);
figure(‘Position‘[6 72 scrsz(3)/4. scrsz(4)/1.3]);
for i=1:ND
ind(i)=i;
gamma(i)=rho(i)*delta(i);
end
subplot(211)
tt=plot(rho(:)delta(:)‘o‘‘MarkerSize‘5‘MarkerFaceColor‘‘k‘‘MarkerEdgeColor‘‘k‘);
title (‘Decision Graph‘‘FontSize‘15.0)
xlabel (‘\rho‘)
ylabel (‘\delta‘)
subplot(211)
rect = getrect(1);
rhomin=rect(1);
deltamin=rect(2);
NCLUST=0;
for i=1:ND
cl(i)=-1;
end
for i=1:ND
if ( (rho(i)>rhomin) && (delta(i)>deltamin))
NCLUST=NCLUST+1;
cl(i)=NCLUST;
评论
共有 条评论