目录
1. 分层聚类
1.1. 分层聚类是什么
层次聚类(hierarchical clustering)试图在不同层次对数据集进行划分,从而形成树状的聚类结构。数据集的划分可以采用“自底向上”的聚合策略,也可采用“自顶向下”的分拆策略。由于“自底向上”的策略更加通用,在这里只讨论它。
AGNES(AGglomerative NESting)算法是“自底向上”的策略的层次聚类方法,它先将数据集中每个样本作为初始聚类簇,算法在每次迭代过程中找出距离最近(相似性最高)的两个簇进行合并,不断迭代,直到达到预设的聚类簇个数。

这里的距离区别于点对点之间的距离,簇 C i , C j C_i,C_j Ci,Cj之间的距离可以被定义为:
最 小 距 离 : d m i n ( C i , C j ) = min x ∈ C i , z ∈ C j d i s t ( x , z ) (1) 最小距离:d_{min}(C_i,C_j) = \min_{x \in C_i,z \in C_j}dist(x,z) \tag{1} 最小距离:dmin(Ci,Cj)=x∈Ci,z∈Cjmindist(x,z)(1)
最 大 距 离 : d m a x ( C i , C j ) = max x ∈ C i , z ∈ C j d i s t ( x , z ) (2) 最大距离:d_{max}(C_i,C_j) = \max_{x \in C_i,z \in C_j}dist(x,z) \tag{2} 最大距离:dmax(Ci,Cj)=x∈Ci,z∈Cjmaxdist(x,z)(2)
平 均 距 离 : d a v g ( C i , C j ) = 1 ∣ C i ∣ ∣ C j ∣ ∑ x ∈ C i ∑ z ∈ C j d i s t ( x , z ) (3) 平均距离:d_{avg}(C_i,C_j) = \frac{1}{|C_i||C_j|}\sum_{x \in C_i}\sum_{z \in C_j}dist(x,z)\tag{3} 平均距离:davg(Ci,Cj)=∣Ci∣∣Cj∣1x∈Ci∑z∈Cj∑dist(x,z)(3)
集合之间的距离计算通常采用豪斯多夫距离(Hausdorff distance):
豪 斯 多 夫 距 离 : h ( C i , C j ) = max x ∈ C i { min z ∈ C j { d i s t ( x , z ) } } 豪斯多夫距离:h(C_i,C_j) = \max_{x \in C_i}\{\min_{z \in C_j}\{dist(x,z)\}\} 豪斯多夫距离:h(Ci,Cj)=x∈Cimax{ z∈Cjmin{ dist(x,z)}}
A more general definition of Hausdorff distance would be :
H ( C i , C j ) = max { h ( C i , C j ) , h ( C j , C i ) } H(C_i,C_j) = \max\{h(C_i,C_j),h(C_j,C_i)\} H(Ci,Cj)=max{ h(Ci,Cj),h(Cj,Ci)}
伪代码:
1. h = 0
2. for every point ai of A,
2.1 shortest = Inf ;
2.2 for every point bj of B
dij = d (ai , bj )
if dij < shortest then
shortest = dij
2.3 if shortest > h then
h = shortest
当簇之间的距离采用 d m i n , d m a x , d a v g d_{min}, d_{max}, d_{avg} dmin,dmax,davg的时候,AGNES算法相应的被称为单链接(single-linkage),全链接(complete-linkage)或均链接(average-linkage)算法。
In Single-Link clustering similarity between clusters is measured as the similarity between the most similar pair of elements, one from each of the clusters, while in Complete-Link clustering the similarity is measured using the least similar pair of elements.
similarity between clusters is measured as the similarity between the most similar pair of elements, one from each of the clusters, while in Complete-Link clustering the similarity is measured using the least similar pair of elements.
2.1. 分层聚类代码实现
伪代码:

# 自底向上层次聚类算法
import tsplib95
import numpy as np
import sys
# 预设聚类簇数
k = 6
# 距离计算函数 0: max, 1: min; 2: average; 3: hausdorff
dist_func = 3
# 聚类簇距离度量函数
def get_cluster_distance(cluster1, cluster2, problem):
dist = 0
if dist_func == 0:
dist = get_distance_max(cluster1, cluster2, problem)
elif dist_func == 1:
dist = get_distance_min(cluster1, cluster2, problem)
elif dist_func == 2:
dist = get_distance_average(cluster1, cluster2, problem)
elif dist_func == 3:
dist = get_distance_hausdorff(cluster1, cluster2, problem)
else:
print("dist_func is not follow standard!")
return dist
def get_node_distance(node1, node2, problem):
return problem.get_weight(

本文介绍了分层聚类的基本概念及其实现方法,并详细探讨了随机游走在聚类任务中的应用,包括其定义、概率矩阵计算及作为优化手段的使用方式。
最低0.47元/天 解锁文章
2543

被折叠的 条评论
为什么被折叠?



