经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » MATLAB » 查看文章
密度峰值聚类算法MATLAB程序
来源:cnblogs  作者:凯鲁嘎吉  时间:2019/10/10 8:53:47  对本文有异议

密度峰值聚类算法MATLAB程序

凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/

密度峰值聚类算法简介见:[转] 密度峰值聚类算法(DPC)

数据见:MATLAB中“fitgmdist”的用法及其GMM聚类算法,保存为gauss_data.txt文件,数据最后一列是类标签。

1. MATLAB程序

  1. clear all
  2. close all
  3. data_load=dlmread('gauss_data.txt');
  4. [num,dim]=size(data_load); %数据最后一列是类标签
  5. data=data_load(:,1:dim-1); %去掉标签的数据
  6. mdist=pdist(data); %两两行之间距离
  7. A=tril(ones(num))-eye(num);
  8. [x,y]=find(A~=0);
  9. % Column 1: id of element i, Column 2: id of element j', Column 3: dist(i,j)'
  10. xx=[x y mdist'];
  11. ND=max(xx(:,2));
  12. NL=max(xx(:,1));
  13. if (NL>ND)
  14. ND=NL;
  15. end
  16. N=size(xx,1);
  17. for i=1:ND
  18. for j=1:ND
  19. dist(i,j)=0;
  20. end
  21. end
  22. for i=1:N
  23. ii=xx(i,1);
  24. jj=xx(i,2);
  25. dist(ii,jj)=xx(i,3);
  26. dist(jj,ii)=xx(i,3);
  27. end
  28. percent=2.0;
  29. fprintf('average percentage of neighbours (hard coded): %5.6f\n', percent);
  30.  
  31. position=round(N*percent/100);
  32. sda=sort(xx(:,3));
  33. dc=sda(position);
  34.  
  35. fprintf('Computing Rho with gaussian kernel of radius: %12.6f\n', dc);
  36.  
  37. for i=1:ND
  38. rho(i)=0.;
  39. end
  40. %
  41. % Gaussian kernel
  42. %
  43. for i=1:ND-1
  44. for j=i+1:ND
  45. rho(i)=rho(i)+exp(-(dist(i,j)/dc)*(dist(i,j)/dc));
  46. rho(j)=rho(j)+exp(-(dist(i,j)/dc)*(dist(i,j)/dc));
  47. end
  48. end
  49. %
  50. % "Cut off" kernel
  51. %
  52. %for i=1:ND-1
  53. % for j=i+1:ND
  54. % if (dist(i,j)<dc)
  55. % rho(i)=rho(i)+1.;
  56. % rho(j)=rho(j)+1.;
  57. % end
  58. % end
  59. %end
  60.  
  61. maxd=max(max(dist));
  62.  
  63. [rho_sorted,ordrho]=sort(rho,'descend');
  64. delta(ordrho(1))=-1.;
  65. nneigh(ordrho(1))=0;
  66.  
  67. for ii=2:ND
  68. delta(ordrho(ii))=maxd;
  69. for jj=1:ii-1
  70. if(dist(ordrho(ii),ordrho(jj))<delta(ordrho(ii)))
  71. delta(ordrho(ii))=dist(ordrho(ii),ordrho(jj));
  72. nneigh(ordrho(ii))=ordrho(jj);
  73. end
  74. end
  75. end
  76. delta(ordrho(1))=max(delta(:));
  77. disp('Generated file:DECISION GRAPH')
  78. disp('column 1:Density')
  79. disp('column 2:Delta')
  80.  
  81. fid = fopen('DECISION_GRAPH', 'w');
  82. for i=1:ND
  83. fprintf(fid, '%6.2f %6.2f\n', rho(i),delta(i));
  84. end
  85.  
  86. disp('Select a rectangle enclosing cluster centers')
  87. scrsz = get(0,'ScreenSize');
  88. figure('Position',[6 72 scrsz(3)/4. scrsz(4)/1.3]);
  89. for i=1:ND
  90. ind(i)=i;
  91. gamma(i)=rho(i)*delta(i);
  92. end
  93. subplot(2,1,1)
  94. tt=plot(rho(:),delta(:),'o','MarkerSize',5,'MarkerFaceColor','k','MarkerEdgeColor','k');
  95. title ('Decision Graph','FontSize',15.0)
  96. xlabel ('\rho')
  97. ylabel ('\delta')
  98.  
  99. fig=subplot(2,1,1);
  100. rect = getrect(fig);
  101. rhomin=rect(1);
  102.  
  103. deltamin=rect(2);
  104. NCLUST=0;
  105. for i=1:ND
  106. cl(i)=-1;
  107. end
  108. for i=1:ND
  109. if ( (rho(i)>rhomin) && (delta(i)>deltamin))
  110. NCLUST=NCLUST+1;
  111. cl(i)=NCLUST;
  112. icl(NCLUST)=i;
  113. end
  114. end
  115. fprintf('NUMBER OF CLUSTERS: %i \n', NCLUST);
  116. disp('Performing assignation')
  117.  
  118. %assignation
  119. for i=1:ND
  120. if (cl(ordrho(i))==-1)
  121. cl(ordrho(i))=cl(nneigh(ordrho(i)));
  122. end
  123. end
  124. %halo
  125. for i=1:ND
  126. halo(i)=cl(i);
  127. end
  128. if (NCLUST>1)
  129. for i=1:NCLUST
  130. bord_rho(i)=0.;
  131. end
  132. for i=1:ND-1
  133. for j=i+1:ND
  134. if ((cl(i)~=cl(j))&& (dist(i,j)<=dc))
  135. rho_aver=(rho(i)+rho(j))/2.;
  136. if (rho_aver>bord_rho(cl(i)))
  137. bord_rho(cl(i))=rho_aver;
  138. end
  139. if (rho_aver>bord_rho(cl(j)))
  140. bord_rho(cl(j))=rho_aver;
  141. end
  142. end
  143. end
  144. end
  145. for i=1:ND
  146. if (rho(i)<bord_rho(cl(i)))
  147. halo(i)=0;
  148. end
  149. end
  150. end
  151. for i=1:NCLUST
  152. nc=0;
  153. nh=0;
  154. for j=1:ND
  155. if (cl(j)==i)
  156. nc=nc+1;
  157. end
  158. if (halo(j)==i)
  159. nh=nh+1;
  160. end
  161. end
  162. fprintf('CLUSTER: %i CENTER: %i ELEMENTS: %i CORE: %i HALO: %i \n', i,icl(i),nc,nh,nc-nh);
  163. end
  164.  
  165. cmap=colormap;
  166. for i=1:NCLUST
  167. ic=int8((i*64.)/(NCLUST*1.));
  168. subplot(2,1,1)
  169. hold on
  170. plot(rho(icl(i)),delta(icl(i)),'o','MarkerSize',8,'MarkerFaceColor',cmap(ic,:),'MarkerEdgeColor',cmap(ic,:));
  171. end
  172. subplot(2,1,2)
  173. disp('Performing 2D nonclassical multidimensional scaling')
  174. Y1 = mdscale(dist, 2, 'criterion','metricstress');
  175. plot(Y1(:,1),Y1(:,2),'o','MarkerSize',2,'MarkerFaceColor','k','MarkerEdgeColor','k');
  176. title ('2D Nonclassical multidimensional scaling','FontSize',15.0)
  177. xlabel ('X')
  178. ylabel ('Y')
  179. for i=1:ND
  180. A(i,1)=0.;
  181. A(i,2)=0.;
  182. end
  183. for i=1:NCLUST
  184. nn=0;
  185. ic=int8((i*64.)/(NCLUST*1.));
  186. for j=1:ND
  187. if (halo(j)==i)
  188. nn=nn+1;
  189. A(nn,1)=Y1(j,1);
  190. A(nn,2)=Y1(j,2);
  191. end
  192. end
  193. hold on
  194. plot(A(1:nn,1),A(1:nn,2),'o','MarkerSize',2,'MarkerFaceColor',cmap(ic,:),'MarkerEdgeColor',cmap(ic,:));
  195. end
  196.  
  197. %for i=1:ND
  198. % if (halo(i)>0)
  199. % ic=int8((halo(i)*64.)/(NCLUST*1.));
  200. % hold on
  201. % plot(Y1(i,1),Y1(i,2),'o','MarkerSize',2,'MarkerFaceColor',cmap(ic,:),'MarkerEdgeColor',cmap(ic,:));
  202. % end
  203. %end
  204. faa = fopen('CLUSTER_ASSIGNATION', 'w');
  205. disp('Generated file:CLUSTER_ASSIGNATION')
  206. disp('column 1:element id')
  207. disp('column 2:cluster assignation without halo control')
  208. disp('column 3:cluster assignation with halo control')
  209. for i=1:ND
  210. fprintf(faa, '%i %i %i\n',i,cl(i),halo(i));
  211. end

2. 结果

原文链接:http://www.cnblogs.com/kailugaji/p/11643859.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号