处理输入为非对角阵的Clustering by fast search and find of density peak代码
Clustering by fast search and find of density peak. Alex Rodriguez, Alessandro Laio
是发表在Science上的一篇很好的阐述一种新聚类算法的paper,其自带代码
http://www.sciencemag.org/content/suppl/2014/06/25/344.6191.1492.DC1/1242072DataS1.zip
展示了该代码的实现过程和使用结果。
不过,该测试代码要求输入为一个格式特殊的三角矩阵,为了使用方便,这里我把源代码做少量修改,
增加预处理部分,使得可以直接处理标准的距离矩阵格式数据
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(i,j)')
% mdist=input('name of the distance matrix file (with single quotes)?\n');
% disp('Reading input distance matrix')
% xx=load(mdist);
% xx = load('D:\example_distances.dat')
%x = load('C:\UseTraceOfAllUsers.txt');
x = load('D:\Courses\Cloud Computing\Clustering\dataWaitingForClustering4.2.txt');
minX = min(x);
maxX = max(x);
ran = maxX - minX;
nx(:,1) = (x(:,1) - minX(1,1)) / ran(1,1);
nx(:,2) = (x(:,2) - minX(1,2)) / ran(1,2);
dist = pdist2(nx, nx);
N = size(dist,1);
xx = zeros((N-1)*N/2, 3);
idx = 1;
for i=1:N
for j=i+1:N
xx(idx, 1) = i;
xx(idx, 2) = j;
xx(idx, 3) = dist(i, j);
idx = idx + 1;
end
end
N = size(xx, 1);
ND=max(xx(:,2));
NL=max(xx(:,1));
if (NL>ND)
ND=NL;
end
% N=size(xx,1);
% for i=1:ND
% for j=1:ND
% dist(i,j)=0;
% end
% end
% for i=1:N
% ii=xx(i,1);
% jj=xx(i,2);
% dist(ii,jj)=xx(i,3);
% dist(jj,ii)=xx(i,3);
% end
% percent=2;
% fprintf('average percentage of neighbours (hard coded): %5.6f\n', percent);
%
% position=round(N*percent/100);
% sda=sort(xx(:,3));
% dc=sda(position);
dc = 0.15; 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(i,j)/dc)*(dist(i,j)/dc));
rho(j)=rho(j)+exp(-(dist(i,j)/dc)*(dist(i,j)/dc));
end
end
%
% "Cut off" kernel
%
% for i=1:ND-1
% for j=i+1:ND
% if (dist(i,j)<dc)S
% rho(i)=rho(i)+1.;
% rho(j)=rho(j)+1.;
% end
% end
% end maxd=max(max(dist)); [rho_sorted,ordrho]=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)))
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(2,1,1)
tt=plot(rho(:),delta(:),'o','MarkerSize',5,'MarkerFaceColor','k','MarkerEdgeColor','k');
title ('Decision Graph','FontSize',15.0)
xlabel ('\rho')
ylabel ('\delta') subplot(2,1,1)
rect = getrect(1);
rhomin=rect(1);
deltamin=rect(4);
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;
icl(NCLUST)=i;
end
end
fprintf('NUMBER OF CLUSTERS: %i \n', NCLUST);
disp('Performing assignation') %assignation
for i=1:ND
if (cl(ordrho(i))==-1)
cl(ordrho(i))=cl(nneigh(ordrho(i)));
end
end
%halo
for i=1:ND
halo(i)=cl(i);
end
if (NCLUST>1)
for i=1:NCLUST
bord_rho(i)=0.;
end
for i=1:ND-1
for j=i+1:ND
if ((cl(i)~=cl(j))&& (dist(i,j)<=dc))
rho_aver=(rho(i)+rho(j))/2.;
if (rho_aver>bord_rho(cl(i)))
bord_rho(cl(i))=rho_aver;
end
if (rho_aver>bord_rho(cl(j)))
bord_rho(cl(j))=rho_aver;
end
end
end
end
for i=1:ND
if (rho(i)<bord_rho(cl(i)))
halo(i)=0;
end
end
end
for i=1:NCLUST
nc=0;
nh=0;
for j=1:ND
if (cl(j)==i)
nc=nc+1;
end
if (halo(j)==i)
nh=nh+1;
end
end
fprintf('CLUSTER: %i CENTER: %i ELEMENTS: %i CORE: %i HALO: %i \n', i,icl(i),nc,nh,nc-nh);
end cmap=colormap;
for i=1:NCLUST
ic=int8((i*64.)/(NCLUST*1.));
subplot(2,1,1)
hold on
plot(rho(icl(i)),delta(icl(i)),'o','MarkerSize',8,'MarkerFaceColor',cmap(ic,:),'MarkerEdgeColor',cmap(ic,:));
end
subplot(2,1,2)
disp('Performing 2D nonclassical multidimensional scaling')
Y1 = mdscale(dist, 2, 'criterion','metricstress');
plot(Y1(:,1),Y1(:,2),'o','MarkerSize',2,'MarkerFaceColor','k','MarkerEdgeColor','k');
title ('2D Nonclassical multidimensional scaling','FontSize',15.0)
xlabel ('X')
ylabel ('Y')
for i=1:ND
A(i,1)=0.;
A(i,2)=0.;
end
for i=1:NCLUST
nn=0;
ic=int8((i*64.)/(NCLUST*1.));
for j=1:ND
if (halo(j)==i)
nn=nn+1;
A(nn,1)=Y1(j,1);
A(nn,2)=Y1(j,2);
end
end
hold on
plot(A(1:nn,1),A(1:nn,2),'o','MarkerSize',2,'MarkerFaceColor',cmap(ic,:),'MarkerEdgeColor',cmap(ic,:));
end %for i=1:ND
% if (halo(i)>0)
% ic=int8((halo(i)*64.)/(NCLUST*1.));
% hold on
% plot(Y1(i,1),Y1(i,2),'o','MarkerSize',2,'MarkerFaceColor',cmap(ic,:),'MarkerEdgeColor',cmap(ic,:));
% end
%end
faa = fopen('CLUSTER_ASSIGNATION', 'w');
disp('Generated file:CLUSTER_ASSIGNATION')
disp('column 1:element id')
disp('column 2:cluster assignation without halo control')
disp('column 3:cluster assignation with halo control')
for i=1:ND
fprintf(faa, '%i %i %i\n',i,cl(i),halo(i));
end
处理输入为非对角阵的Clustering by fast search and find of density peak代码的更多相关文章
- Science14年的聚类论文——Clustering by fast search and find of density peaks
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 这是一个比较新的聚类方法(文章中没看见作者对其取名,在这里我姑且称该方法为local density clu ...
- Science论文"Clustering by fast search and find of density peaks"学习笔记
"Clustering by fast search and find of density peaks"是今年6月份在<Science>期刊上发表的的一篇论文,论文中 ...
- 一种新型聚类算法(Clustering by fast search and find of density peaksd)
最近在学习论文的时候发现了在science上发表的关于新型的基于密度的聚类算法 Kmean算法有很多不足的地方,比如k值的确定,初始结点选择,而且还不能检测费球面类别的数据分布,对于第二个问题,提出了 ...
- Clustering by fast search and find of density peaks
参考:http://www.52ml.net/16296.html 这个算法的优点就在于,它首先一步就能找到聚类中心,然后划分类别.而其他算法需要反复迭代才能找到中心聚类. 就是不知道代码该怎么写.. ...
- Clustering and Exploring Search Results using Timeline Constructions (paper2)
作者:Omar Alonso 会议:CIKM 2009 摘要: 截至目前(2009),通过提取文档中内嵌的时间信息来展现和聚类,这方面的工作并不多. 在这篇文章中,我们将提出一个“小插件”增添到现有的 ...
- 解读论文《Agglomerative clustering of a search engine query log》,以解决搜索推荐相关问题
<Agglomerative clustering of a search engine query log> 论文作者:Doug Beeferman 本文将解读此篇论文,此论文利用搜索日 ...
- C++学习---二叉树的输入及非递归遍历
二叉树的二叉链表存储表示如下 //二叉树的二叉链表存储表示 typedef struct BiTNode { char data;//结点数据域 struct BiTNode* lchild, * r ...
- Linux中让终端输入变为非阻塞的三种方法
介绍 在linux下每打开一个终端,系统自动的就打开了三个文件,它们的文件描述符分别为0,1,2,功能分别是"标准输入"."标准输出"和"标准错误输出 ...
- python(38):sys.argv,sys.argv.pop(),获取用户的外部输入,非指定
见下面的例子(一): # /usr/bin/env python # coding=utf8 import os import requests import sys if __name__ == & ...
随机推荐
- 【fiddler】Fiddller的应用
一.fiddler抓取移动端接口 1.获取PC端IP 2.手机ip设置为与电脑同一局域网ip并配置代理 1)手机ip地址与pc地址连接同一局域网网络 2)代理设置为手动,主机名为PCip,端口号为88 ...
- Win8电脑更新出现错误代码80070003的解决方法
有Win8系统用户在进行KB2894853更新时会碰到系统报错的问题,错误代码是80070003,那么在遇到这个问题的时候,我们该怎么去解决呢?下面好系统U盘启动就来告诉你相应的解决方法. Win8系 ...
- Swift Review总结一:从 Swift Style 开始
最近凑了几个热心的小伙伴写一些Swift的新手demo(两周后应该能和大家见面了),我参与了review.于是借demo里的代码总结一下新手写Swift要注意的问题,尤其是从oc转到用swift写的开 ...
- MinGW-W64 编译 LLVM 与 Clang
原文: http://blog.csdn.net/happywjh666/article/details/51415723 编译环境: 系统 --win10 64位 gcc -- version 5. ...
- 02_Hive安装简介
1.下载Hive安装包: 官网下载:http://hive.apache.org/downloads.html 百度云分享:https://pan.baidu.com/s/1M4LmdOXaq6T-P ...
- python_tkinter组件摆放方式
1.最小界面组成 # 导入tkinter模块 import tkinter # 创建主窗口对象 root = tkinter.Tk() # 设置窗口大小(最小值:像素) root.minsize(30 ...
- No provider available from registry出错
dubbo+zookeeper进行分布式远程调用时No provider available from registry出错 查看dubbo服务:http://192.168.0.100:8080/d ...
- VSCODE常用插件使用记录
常用必备: 1.vscode-icon 让 vscode 资源树目录加上图标,必备良品! 2.Path Intellisense 自动路劲补全,默认不带这个功能的 3.beautify Beautif ...
- mongodb命令----批量更改文档字段名
因为mongodb基于javascript的特性,为了体验cursor的威力我们不妨利用js的for循环创建记录 先创建文档 db.createCollection("columnsampl ...
- [RxJS] RxJS Advanced Patterns Operate Heavily Dynamic UIs
Check the playground. import {Counter, CountDownState, ConterStateKeys, PartialCountDownState} from ...