处理输入为非对角阵的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__ == & ...
随机推荐
- C++中volatile
volatile只保证其“可见性”,不保证其“原子性”. 执行count++;这条语句由3条指令组成: (1)将 count 的值从内存加载到 cpu 的某个 寄存器r: (2)将 寄存器r 的值 + ...
- OverflowError:django signed integer is greater than maximum 数据库日期字段相关错
使用django中的默认数据库sqlite3, 在pycharm中录入日期字段相关信息结果出现问题 在保存的时候如图 直接在界面选择的日期变成了时间戳, 并且在获取数据的时候报错 经过查询之后(舔大佬 ...
- 解决docker容器开启端口映射后,会自动在防火墙上打开端口的问题
在docker中运行第三方服务时,通常需要绑定服务端口到本地主机.但使用 -p 参数进行的端口映射,会自动在iptables中建立规则,绕过firewalld,这对于端口级的黑白名单控制管理是很不利的 ...
- web开发:css总结与应用
一.常用标签的使用 二.边界圆角 三.背景样式 四.精灵图 五.盒模型布局细节 六.盒模型案例 七.w3c主页 一.常用标签的使用 <!DOCTYPE html> <html> ...
- 在idea中相同的字符串使用equals()进行比较时,返回值是flase问题
最近在idea中遇到了一个编码的问题,我的程序是从前台传过来一个字符串,判断用户的角色(学生,教师,管理员), 在进行equals()判断时,返回的确是false,然后就在网上查了查,发现是编码的问题 ...
- TCP协议之网络延时
影响TCP 网络时延的因素硬件速度网络和服务器的负载请求和响应报文的尺寸客户端和服务器之间的距离TCP 协议的技术复杂性TCP协议产生的时延TCP 连接建立握手:TCP 慢启动拥塞控制:数据聚集的 N ...
- 数字证书注册审批机构(RA)
1.证书注册审批机构 RA(Registration Authority),数字证书注册审批机构.RA系统是CA的证书发放.管理的延伸.它负责证书申请者的信息录入.审核以及证书发放等工作:同时,对发放 ...
- 内核对象&句柄&泄漏&检测
今天看到这个问题如何评价王垠的 <讨厌的 C# IDisposable 接口>? - 王垠(人物),答案被歪到windows 内核对象和句柄,答案中谈的太浅显而且有误.翻出陈年老文章(此文 ...
- PHP常见设计模式
简单工厂 <?php # 简单工厂 abstract class mycoach { public function __construct(){} } class cultivatecpc e ...
- [Luogu] 时间复杂度
https://www.luogu.org/problemnew/show/P3952 考场上输出的是 "YES" "NO" ++ ,如果不是亲身经历,打死我我 ...