章节概述
层次聚类:一种构建树结构(树状图)的聚类算法,显示不同尺度下的嵌套分组。与K-Means或GMM不同,它不需要事先指定K。
两种方法:聚合式(自底向上):从单个点开始,合并最近的簇。分裂式(自顶向下):从一个簇开始,递归分割。
关键挑战:定义簇之间的"距离"(连接方法):单连接、完全连接、平均连接、质心连接或Ward方法。不同的连接方法产生不同的层次结构。
23.1 层次聚类介绍
🌳 构建层次结构
层次聚类创建树状结构,同时显示所有尺度上的关系。 树状图可视化揭示细粒度结构(许多小簇)和粗粒度结构(少数大簇)。
- • 开始:每个点都是自己的簇
- • 迭代:合并两个最近的簇
- • 结束:所有点在一个簇中
- • 结果:从叶到根的树状图
- • 开始:所有点在一个簇中
- • 迭代:将簇分成两个
- • 结束:每个点都是自己的簇
- • 结果:从根到叶的树状图
优点与缺点
- • 无需指定K:可在任何层级探索层次结构
- • 确定性:每次运行结果相同(无随机初始化)
- • 视觉解释:树状图清晰显示结构
- • 灵活性:多种距离/连接方法
- • 嵌套簇:揭示层次关系
- • 计算成本:聚合式为O(n²log n)
- • 不可扩展:大数据集(>10k点)处理困难
- • 贪婪合并:无法撤销早期错误决策
- • 连接敏感性:不同方法结果差异显著
- • 距离假设:需要有意义的距离度量
23.2 树状图构建:逐步演示
📊 树状图如何构建
树状图逐层构建,从单个数据点开始。 在每一层,我们合并距离最小的两个簇。 树状图中的垂直位置表示合并距离。
树状图构建:逐步演示
观察层次聚类如何自底向上构建树
Data Points & Clusters
Dendrogram (Tree)
Distance Matrix Heatmap
🌳 Bottom-Up Agglomerative Clustering:Start with each data point as its own cluster. At each layer, merge the two closest clusters (single linkage: nearest neighbor distance). Continue until all points belong to one cluster. The dendrogram visualizes this hierarchical structure, where vertical position indicates merge distance.
读取树状图
23.3 连接方法:簇距离定义
📏 核心挑战
虽然计算两个单独点之间的距离很简单, 但我们如何测量包含多个点的两个簇之间的距离? 不同的连接方法给出不同的答案,导致不同的层次结构。
Linkage Method Comparison
Different ways to measure distance between clusters
Select Linkage Method:
Visual Explanation
Single Linkage (Nearest Neighbor)
Distance between two clusters is the minimum distance between any two points from the two clusters.
Visual Key:
📏 Choosing Linkage Method:Different linkage methods produce different clustering results.Single linkage can capture elongated clusters but is sensitive to noise.Complete linkage prefers compact clusters.Average linkage balances the two extremes.Ward's method minimizes within-cluster variance (like K-Means) and is the default in sklearn.Centroid linkage is intuitive but may produce non-monotonic dendrograms.
单连接 vs 完全连接
Ward方法(默认)
Ward连接是sklearn中的默认方法,通常产生最佳结果。 它将合并两个簇的"成本"衡量为总簇内方差(SSE)的增加。
Ward方法在每次合并时最小化总簇内方差的增加, 类似于K-Means的目标。它倾向于创建平衡、紧凑的簇。
23.4 交互式探索
Interactive Hierarchical Clustering
Adjust cut height to control number of clusters
Scatter Plot & Clustering
Dendrogram with Cut Line
Reordered Distance Matrix
🎛️ Interactive Exploration:Hierarchical clustering doesn't require specifying K beforehand. Instead, you build a complete hierarchy and then choose where to "cut" based on your needs. Try different cut heights: low values preserve fine structure (many small clusters), high values capture only major groups (few large clusters). The dendrogram visually shows the hierarchical relationships at all scales simultaneously.
实际应用
层次聚类因其揭示多尺度结构的能力而在各个领域广泛应用: