# 凝聚层次聚类

凝聚层次聚类是一种自底向上的聚类方法：它从每个数据点各自成为一个簇开始，反复合并最近的两个簇，直到只剩下一个簇，从而产生嵌套分组构成的层次结构，而不是一个扁平的划分。<sup>[1](https://ar5iv.labs.arxiv.org/html/1012.3697)</sup> 结果是对于 \( n \) 个点恰好有 \( n - 1 \) 次合并的二叉合并树，通常画成树状图；在选定高度剪断该树，就得到任意组数的扁平聚类。<sup>[2](https://franknielsen.github.io/Clustering/BookChapter-HierarchicalClustering.pdf)</sup> 该方法是确定性的、贪心的，各种方案的区别只在于如何定义两个簇之间的距离以及每次合并后如何更新该距离。<sup>[3](https://ar5iv.labs.arxiv.org/html/1109.2378)</sup>

| 关键事实 | 细节 |
|---|---|
| 输出 | 由 n − 1 次合并得到的树状图（二叉合并树）；扁平簇来自剪断它<sup>[2](https://franknielsen.github.io/Clustering/BookChapter-HierarchicalClustering.pdf)</sup> |
| 合并规则 | 每一步合并簇间相异度最小的一对簇<sup>[3](https://ar5iv.labs.arxiv.org/html/1109.2378)</sup> |
| 常见连接方式 | 单连接、完全连接、平均连接（UPGMA）、加权平均连接（WPGMA）、沃德（Ward）、质心、中位数<sup>[3](https://ar5iv.labs.arxiv.org/html/1109.2378)</sup> |
| 沃德准则 | Δ(A,B) = (n_A n_B/(n_A+n_B))·‖x̄_A − x̄_B‖²，即簇内平方和的增量；仅适用于欧几里得数据<sup>[4](https://lanselin.github.io/introbook_vol2/agglomerative-clustering.html)</sup> |
| 复杂度 | 朴素 Θ(N³)；最近邻链和基于最小生成树的算法为 O(N²)<sup>[5](https://nlp.stanford.edu/IR-book/pdf/17hier.pdf)</sup><sup> • </sup><sup>[3](https://ar5iv.labs.arxiv.org/html/1109.2378)</sup> |
| 选择 k | 在阈值处、在合并高度最大间隙处剪断树状图，或在距离阈值处停止<sup>[6](https://www.math.kth.se/matstat/gru/sf2935/sf2935lect17a.pdf)</sup><sup> • </sup><sup>[7](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.AgglomerativeClustering.html)</sup> |
| 软件默认 | scikit-learn默认使用沃德法；R的hclust为沃德的实际准则提供ward.D2<sup>[7](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.AgglomerativeClustering.html)</sup><sup> • </sup><sup>[8](https://search.r-project.org/R/refmans/stats/html/hclust.html)</sup> |

## 工作原理

该算法是贪心的：每一步合并当前最近的一对簇，并且从不重新审视某次合并。“最近”的含义由连接准则决定。单连接取所有跨簇对上的最小相异度，完全连接取最大相异度，平均连接（UPGMA）取所有 n_A·n_B 个跨簇对上的均值。<sup>[9](https://www.stat.cmu.edu/~ryantibs/datamining/lectures/05-clus2.pdf)</sup><sup> • </sup><sup>[4](https://lanselin.github.io/introbook_vol2/agglomerative-clustering.html)</sup> 加权平均（WPGMA）以及质心和中位数方案补足了七种最常见的方法。<sup>[3](https://ar5iv.labs.arxiv.org/html/1109.2378)</sup> 前四种只作用于相异度矩阵，而沃德法、质心法和中位数法是几何方法，需要欧几里得坐标。<sup>[10](https://jmlr.org/papers/volume19/18-117/18-117.pdf)</sup>

合并之后，更新后的距离通常用兰斯–威廉姆斯（Lance–Williams）递推公式计算，

\[ d(I \cup J, K) = \alpha_{I}\, d(I,K) + \alpha_{J}\, d(J,K) + \beta\, d(I,J) + \gamma\, |d(I,K) - d(J,K)|, \]

该公式由兰斯（Lance）和威廉姆斯（Williams）于1967年提出；令 \( \alpha_{I} = \alpha_{J} = 1/2 \)、\( \beta = 0 \) 且 \( \gamma = -1/2 \) 就得到单连接，而每种连接都对应自己的系数。<sup>[3](https://ar5iv.labs.arxiv.org/html/1109.2378)</sup>

**沃德法**将簇 A 和 B 之间的距离定义为合并它们所引起的簇内平方和总量的增量，<sup>[11](https://www.stat.cmu.edu/~cshalizi/350/lectures/08/lecture-08.pdf)</sup>

\[ \Delta(A,B) = \frac{n_{A}\, n_{B}}{n_{A}+n_{B}} \lVert \bar{x}_{A} - \bar{x}_{B} \rVert^{2}, \]

其中 n_A 和 n_B 是簇的大小，x̄_A、x̄_B 是它们的质心。<sup>[4](https://lanselin.github.io/introbook_vol2/agglomerative-clustering.html)</sup> 这个量恰好是合并这两个簇所引起的 k均值代价增量，<sup>[12](https://proceedings.neurips.cc/paper_files/paper/2003/file/f7696a9b362ac5a51c3dc8f098b73923-Paper.pdf)</sup> 并且根据惠更斯（Huyghens）方差分解，最小化簇内方差等同于最大化簇间方差。<sup>[13](https://arxiv.org/pdf/1111.6285.pdf)</sup>

## 如何实施

实践者首先选择距离度量和连接方式。沃德法、质心法和中位数法只有在欧几里得度量下才有正确定义；<sup>[14](https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.hierarchy.linkage.html)</sup> 单连接、完全连接和平均连接可作用于任何相异度矩阵，并产生没有逆序的树状图。<sup>[9](https://www.stat.cmu.edu/~ryantibs/datamining/lectures/05-clus2.pdf)</sup> 然后运行合并，通过在阈值相异度值处剪断树状图得到实际聚类。<sup>[6](https://www.math.kth.se/matstat/gru/sf2935/sf2935lect17a.pdf)</sup> 剪断规则包括预先指定的相似度水平，或相邻合并距离之间间隙最大的高度。<sup>[5](https://nlp.stanford.edu/IR-book/pdf/17hier.pdf)</sup> 对沃德法，一条经验法则是不断减小 k，直到合并代价跳变，然后取跳变之前的 k。<sup>[11](https://www.stat.cmu.edu/~cshalizi/350/lectures/08/lecture-08.pdf)</sup>

在软件中，scikit-learn默认使用沃德法，并且对它只接受 euclidean 或 l2 度量；<sup>[7](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.AgglomerativeClustering.html)</sup> SciPy 返回一个 \( (n-1) \times 4 \) 的连接矩阵，包含被合并簇的索引、合并距离和簇大小。<sup>[14](https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.hierarchy.linkage.html)</sup> 在 R 的 hclust 中，选项 "ward.D" 并未实现沃德1963年的准则，而 "ward.D2" 实现了，依据默塔（Murtagh）和勒让德（Legendre）（2014）。<sup>[8](https://search.r-project.org/R/refmans/stats/html/hclust.html)</sup>

## 起源

凝聚聚类的历史至少可以追溯到20世纪50年代，生物分类学是推动力之一，因为最早使用计算机对生物进行分类的生物学家讨论了几种凝聚方法。<sup>[1](https://ar5iv.labs.arxiv.org/html/1012.3697)</sup> 沃德的最小方差方法发表在乔·H·沃德（Joe H. Ward）的《为优化目标函数而进行层次分组》（"Hierarchical Grouping to Optimize an Objective Function"）一文中，载于《美国统计协会杂志》（Journal of the American Statistical Association），1963年。<sup>[15](https://doi.org/10.1080/01621459.1963.10500845)</sup> 麦奎蒂（McQuitty）的加权（WPGMA）连接来自路易斯·L·麦奎蒂（Louis L. McQuitty）1966年发表于《[教育与心理测量](https://www.edgepedia.org/zh/educational-and-psychological-measurement)》（Educational and Psychological Measurement）的论文。<sup>[16](https://doi.org/10.1177/001316446602600402)</sup> 相异度更新形式很简洁，尽管原论文并未考虑沃德准则；沃德算法是在兰斯–威廉姆斯框架下用平方相异度写出的。<sup>[13](https://arxiv.org/pdf/1111.6285.pdf)</sup> 高尔（Gower）和罗斯（Ross）（1969）观察到单连接与最小生成树有关；这类方法被概括为 SAHN（顺序、凝聚、层次、非重叠）。<sup>[3](https://ar5iv.labs.arxiv.org/html/1109.2378)</sup>

## 变体

当输入是显式的稠密相异度矩阵时，仅该矩阵就是 Θ(N²)，因为必须处理所有成对相异度，所以运行时间的下界为 Ω(N²)；稀疏图或结构化输入不必存储或处理所有成对相异度，而在稠密输入上的原始算法耗时 Θ(N³)。<sup>[3](https://ar5iv.labs.arxiv.org/html/1109.2378)</sup> 由菲奥恩·默塔（Fionn Murtagh）于1985年描述的最近邻链算法，对单连接、完全连接、平均连接、加权连接和沃德法保证 \( O(N^{2}) \) 的最坏情况时间；<sup>[3](https://ar5iv.labs.arxiv.org/html/1109.2378)</sup> 维护最近邻列表（安德伯格（Anderberg），1973）将最好情况复杂度从 Θ(N³) 降至 Θ(N²)。<sup>[3](https://ar5iv.labs.arxiv.org/html/1109.2378)</sup> SciPy 通过优化的 MST 算法实现单连接，并通过最近邻链实现其他四种主要方法，全部为 \( O(n^{2}) \) 时间和 \( O(n^{2}) \) 内存。<sup>[14](https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.hierarchy.linkage.html)</sup>

对于非常大的数据集，BIRCH 增量地对到来的多维点进行聚类，通常利用其 CF树的内存摘要，在单次扫描中找到良好的簇。<sup>[17](https://dl.acm.org/doi/10.1145/235968.233324)</sup><sup> • </sup><sup>[18](https://research.ibm.com/publications/birch-a-new-data-clustering-algorithm-and-its-applications)</sup> 容噪变体包括威沙特（Wishart）法和 CURE（古哈（Guha）、拉斯托吉（Rastogi）和希姆（Shim），1998）。<sup>[19](https://jmlr.org/papers/volume15/balcan14a/balcan14a.pdf)</sup><sup> • </sup><sup>[20](https://doi.org/10.1145/276305.276312)</sup> 在图数据上，杜利帕拉（Dhulipala）及其同事（2021）给出了首个高效的 Õ(m) 时间精确算法，用于完全连接和 WPGMA 连接的 HAC，以及一个 Õ(n√m) 时间的平均连接精确算法，并带有一个 Õ(m) 的近似平均连接变体。<sup>[21](https://proceedings.mlr.press/v139/dhulipala21a.html)</sup>

近期工作面向 GPU 和近似方法。一种 TSP图方法近似沃德法，在结果相同的情况下，相比最佳精确沃德实现达到 25:1 的加速，并使用 \( O(T \cdot N) \) 内存而非 \( O(N^{2}) \)。<sup>[22](https://link.springer.com/article/10.1186/s40537-024-01053-x)</sup> PANDORA 是一种完全并行、工作最优的 \( O(n \log n) \) GPU 算法，通过 MST 上的递归树收缩构造单连接树状图，在 AMD MI250X 上加速 6–20 倍，在 Nvidia A100 上加速 10–37 倍。<sup>[23](https://arxiv.org/html/2401.06089)</sup>

## 应用

平均连接是分析基因表达数据的标准工具。<sup>[12](https://proceedings.neurips.cc/paper_files/paper/2003/file/f7696a9b362ac5a51c3dc8f098b73923-Paper.pdf)</sup> 生物[分类学](https://www.edgepedia.org/zh/taxonomy-biology)在历史上曾是该方法的推动力，<sup>[1](https://ar5iv.labs.arxiv.org/html/1012.3697)</sup> 而 BIRCH 已被应用于构建交互式像素分类工具，以及为图像压缩生成初始码本。<sup>[18](https://research.ibm.com/publications/birch-a-new-data-clustering-algorithm-and-its-applications)</sup>

## 局限与替代方案

**单连接**用最相似的成员来定义簇相似度，这是一种局部准则，会遭受链式效应：合并不断加入单个点或点对，形成链。<sup>[5](https://nlp.stanford.edu/IR-book/pdf/17hier.pdf)</sup> **完全连接**使用最不相似的成员，避免了链式效应，但会遭受拥挤和对离群点的敏感性。<sup>[5](https://nlp.stanford.edu/IR-book/pdf/17hier.pdf)</sup><sup> • </sup><sup>[9](https://www.stat.cmu.edu/~ryantibs/datamining/lectures/05-clus2.pdf)</sup> 平均连接取得平衡，但其结果可能在相异度的单调变换下改变。<sup>[9](https://www.stat.cmu.edu/~ryantibs/datamining/lectures/05-clus2.pdf)</sup> 许多经典凝聚算法对噪声不鲁棒；在存在噪声时，威沙特法、CURE 和沃德法更可取，但它们缺乏理论上的鲁棒性保证。<sup>[19](https://jmlr.org/papers/volume15/balcan14a/balcan14a.pdf)</sup>

**逆序。** 质心公式和中位数公式可能产生非单调的树状图，其合并高度会下降，这很难解释；其他五种方法不会如此。<sup>[3](https://ar5iv.labs.arxiv.org/html/1109.2378)</sup><sup> • </sup><sup>[8](https://search.r-project.org/R/refmans/stats/html/hclust.html)</sup> 一部参考著作对沃德法给出了相反的说法，即“沃德 AHC 不是单调的，因为可能存在逆序”。<sup>[2](https://franknielsen.github.io/Clustering/BookChapter-HierarchicalClustering.pdf)</sup> 对于本文所定义的沃德准则，在用平方欧几里得距离实现时，合并高度是单调的；逆序只来自使用不同距离约定的变体或实现。

**与 k均值的关系。** 沃德的合并距离恰好是某次合并带来的 k均值代价增量，<sup>[12](https://proceedings.neurips.cc/paper_files/paper/2003/file/f7696a9b362ac5a51c3dc8f098b73923-Paper.pdf)</sup> 并且对给定 k，沃德的平方和通常大于 k均值的最小值；一个常见技巧是从沃德法找到的簇出发运行 k均值。<sup>[11](https://www.stat.cmu.edu/~cshalizi/350/lectures/08/lecture-08.pdf)</sup> 与 k均值不同，凝聚聚类是贪心且确定性的，并返回一系列嵌套划分；单连接聚类可以处理复杂的簇形状，但只要求分离，不要求紧致。<sup>[11](https://www.stat.cmu.edu/~cshalizi/350/lectures/08/lecture-08.pdf)</sup>

## References

1. [Analysis of Agglomerative Clustering (Algorithmica; preliminary version STACS 2011)](https://ar5iv.labs.arxiv.org/html/1012.3697)
2. [Hierarchical Clustering (book chapter, Frank Nielsen)](https://franknielsen.github.io/Clustering/BookChapter-HierarchicalClustering.pdf)
3. [Modern hierarchical, agglomerative clustering algorithms (Daniel Müllner, arXiv:1109.2378)](https://ar5iv.labs.arxiv.org/html/1109.2378)
4. [An Introduction to Spatial Data Science with GeoDa, vol. 2, §5.3 Agglomerative Clustering (Anselin)](https://lanselin.github.io/introbook_vol2/agglomerative-clustering.html)
5. [Introduction to Information Retrieval, Chapter 17: Hierarchical clustering](https://nlp.stanford.edu/IR-book/pdf/17hier.pdf)
6. [Modern Methods of Statistical Learning sf2935, Hierarchic Clustering (Timo Koski, KTH)](https://www.math.kth.se/matstat/gru/sf2935/sf2935lect17a.pdf)
7. [AgglomerativeClustering, scikit-learn documentation](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.AgglomerativeClustering.html)
8. [R: Hierarchical Clustering (hclust documentation)](https://search.r-project.org/R/refmans/stats/html/hclust.html)
9. [CMU Data Mining Lecture 5: Hierarchical clustering (Tibshirani)](https://www.stat.cmu.edu/~ryantibs/datamining/lectures/05-clus2.pdf)
10. [An Efficient and Effective Generic Agglomerative Hierarchical Clustering Approach (SNK-AHC, JMLR)](https://jmlr.org/papers/volume19/18-117/18-117.pdf)
11. [CMU 36-350 Lecture 8: Hierarchical Clustering (Shalizi)](https://www.stat.cmu.edu/~cshalizi/350/lectures/08/lecture-08.pdf)
12. [An Iterative Improvement Procedure for Hierarchical Clustering (NeurIPS 2003)](https://proceedings.neurips.cc/paper_files/paper/2003/file/f7696a9b362ac5a51c3dc8f098b73923-Paper.pdf)
13. [Ward's Hierarchical Clustering Method: Clustering Criterion and Agglomerative Algorithm (Murtagh & Legendre)](https://arxiv.org/pdf/1111.6285.pdf)
14. [scipy.cluster.hierarchy.linkage, SciPy v1.18.0 Manual](https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.hierarchy.linkage.html)
15. [Joe H. Ward (1963). Hierarchical Grouping to Optimize an Objective Function. Journal of the American Statistical Association.](https://doi.org/10.1080/01621459.1963.10500845)
16. [Louis L. McQuitty (1966). Similarity Analysis by Reciprocal Pairs for Discrete and Continuous Data. Educational and Psychological Measurement.](https://doi.org/10.1177/001316446602600402)
17. [BIRCH: an efficient data clustering method for very large databases (Zhang, Ramakrishnan, Livny; SIGMOD 1996)](https://dl.acm.org/doi/10.1145/235968.233324)
18. [BIRCH: A new data clustering algorithm and its applications (IBM Research / Data Mining and Knowledge Discovery, 1997)](https://research.ibm.com/publications/birch-a-new-data-clustering-algorithm-and-its-applications)
19. [Clustering (robust agglomerative clustering, JMLR)](https://jmlr.org/papers/volume15/balcan14a/balcan14a.pdf)
20. [Sudipto Guha, Rajeev Rastogi, Kyuseok Shim (1998). CURE. ACM SIGMOD Record.](https://doi.org/10.1145/276305.276312)
21. [Hierarchical Agglomerative Graph Clustering in Nearly-Linear Time (Dhulipala, Eisenstat, Łącki, Mirrokni, Shi; ICML 2021)](https://proceedings.mlr.press/v139/dhulipala21a.html)
22. [Fast agglomerative clustering using approximate traveling salesman solutions (Journal of Big Data, 2024)](https://link.springer.com/article/10.1186/s40537-024-01053-x)
23. [PANDORA: A Parallel Dendrogram Construction Algorithm for Single Linkage Clustering on GPU (2024)](https://arxiv.org/html/2401.06089)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Machine learning and neural computation › Machine learning methods › Supervised, unsupervised, and semi-supervised learning › Clustering algorithms

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/agglomerative-hierarchical-clustering

English original: https://www.edgepedia.org/agglomerative-hierarchical-clustering
