In data mining and statistics, hierarchical clustering (also called hierarchical cluster analysis or HCA) is a method of cluster analysis that seeks to build a hierarchy of clusters. Strategies for hierarchical clustering generally fall into two categories:
- Agglomerative: Agglomerative clustering, often referred to as a "bottom-up" approach, begins with each data point as an individual cluster. At each step, the algorithm merges the two most similar clusters based on a chosen distance metric (e.g., Euclidean distance) and linkage criterion (e.g., single-linkage, complete-linkage). This process continues until all data points are combined into a single cluster or a stopping criterion is met. Agglomerative methods are more commonly used due to their simplicity and computational efficiency for small to medium-sized datasets.
- Divisive: Divisive clustering, known as a "top-down" approach, starts with all data points in a single cluster and recursively splits the cluster into smaller ones. At each step, the algorithm selects a cluster and divides it into two or more subsets, often using a criterion such as maximizing the distance between resulting clusters. Divisive methods are less common but can be useful when the goal is to identify large, distinct clusters first.
In general, the merges and splits are determined in a greedy manner. The results of hierarchical clustering for single-linkage and CLINK for complete-linkage clustering. With a heap, the runtime of the general case can be reduced to <math>\mathcal{O}(n^2 \log n)</math> instead of <math>\mathcal{O}(n^3)</math>, at the cost of additional the memory requirements. In many cases, the memory overheads of this approach are too large to make it practically usable. Methods exist which use quadtrees that demonstrate <math>\mathcal{O}(n^2)</math> total running time with <math>\mathcal{O}(n)</math> space.
Divisive clustering with an exhaustive search is <math>\mathcal{O}(2^n)</math>, but it is common to use faster heuristics to choose splits, such as k-means.
Distance metrics
While the linkage criterion determines how dissimilarity between sets of observations is computed, the underlying distance metric determines how dissimilarity between individual observations is measured. Because hierarchical clustering permits any valid measure of distance, the choice of metric is guided by the nature of the data and can have a significant effect on the resulting clustering.
Euclidean distance is the most widely used metric for continuous numerical data. It corresponds to the straight-line distance between two points in Euclidean space and is the default choice in most statistical software:
:<math>d(\mathbf{x},\mathbf{y}) = 1 - \frac{\mathbf{x}\cdot\mathbf{y{\lVert\mathbf{x}\rVert\,\lVert\mathbf{y}\rVert}</math>
Because it disregards vector magnitude, it is frequently used in text mining and document clustering, where documents are represented as high-dimensional term-frequency vectors.
:<math>d(\mathbf{x},\mathbf{y}) = \sqrt{(\mathbf{x}-\mathbf{y})^\top S^{-1} (\mathbf{x}-\mathbf{y})}</math>
Unlike Euclidean distance, it is scale-invariant and is useful when features are correlated or measured in different units.
Hamming distance is used for categorical or binary data and counts the number of positions at which two observations differ. It is commonly applied to genetic sequences and binary feature vectors.
The choice of metric interacts with the choice of linkage criterion. For example, Ward's method and centroid-based linkages are defined in terms of squared Euclidean distances, whereas single-linkage and complete-linkage clustering can be used with any valid metric.
{|class="wikitable"
! Names
! Formula
|-
| Maximum or complete-linkage clustering
| <math> \max_{a\in A,\, b\in B} d(a,b) </math>
|-
| Minimum or single-linkage clustering
| <math> \min_{a\in A,\, b\in B} d(a,b) </math>
|-
| Unweighted average linkage clustering (or UPGMA)
| <math> \frac{1}{|A|\cdot|B|} \sum_{a \in A }\sum_{ b \in B} d(a,b). </math>
|-
| Weighted average linkage clustering (or WPGMA)
| <math> d(i \cup j, k) = \frac{d(i, k) + d(j, k)}{2}. </math>
|-
| Centroid linkage clustering, or UPGMC
| <math>\lVert \mu_A-\mu_B\rVert^2</math> where <math>\mu_A</math> and <math>\mu_B</math> are the centroids of A resp. B.
|-
|Median linkage clustering, or WPGMC
|<math>d(i\cup j, k) = d(m_{i\cup j}, m_k)</math> where <math>m_{i\cup j} = \tfrac{1}{2}\left(m_i + m_j\right)</math>
|-
|Versatile linkage clustering
| <math>\sqrt[p]{\frac{1}{|A|\cdot|B|} \sum_{a \in A }\sum_{ b \in B} d(a,b)^p}, p\neq 0</math>
|-
|Ward linkage, Minimum Increase of Sum of Squares (MISSQ)
|<math>\frac{|A|\cdot|B|}{|A\cup B|} \lVert \mu_A - \mu_B \rVert ^2
= \sum_{x\in A\cup B} \lVert x - \mu_{A\cup B} \rVert^2
- \sum_{x\in A} \lVert x - \mu_{A} \rVert^2
- \sum_{x\in B} \lVert x - \mu_{B} \rVert^2</math>
|-
|Minimum Error Sum of Squares (MNSSQ)
|<math>\max_{x\in A\cup B} \min_{y\in A\cup B} d(x,y)</math>
|-
|Minimum Sum Medoid linkage
|<math>\min_{m\in A\cup B} \sum_{y\in A\cup B} d(m, y)</math> such that m is the medoid of the resulting cluster
|-
|Minimum Sum Increase Medoid linkage
|<math>d(m_A, m_B)</math> where <math>m_A</math>, <math>m_B</math> are the medoids of the previous clusters
|-
| Minimum energy clustering
| <math> \frac {2}{nm}\sum_{i,j=1}^{n,m} \|a_i- b_j\|_2 - \frac {1}{n^2}\sum_{i,j=1}^{n} \|a_i-a_j\|_2 - \frac{1}{m^2}\sum_{i,j=1}^{m} \|b_i-b_j\|_2 </math>
|}
Some of these can only be recomputed recursively (WPGMA, WPGMC), for many a recursive computation with Lance-Williams-equations is more efficient, while for other (Hausdorff, Medoid) the distances have to be computed with the slower full formula. Other linkage criteria include:
- The probability that candidate clusters spawn from the same distribution function (V-linkage).
- The product of in-degree and out-degree on a k-nearest-neighbour graph (graph degree linkage).
- The increment of some cluster descriptor (i.e., a quantity defined for measuring the quality of a cluster) after merging two clusters.
Comparison of linkage methods
The choice of linkage criterion significantly affects the shape and quality
of the resulting clusters. Each method has distinct strengths and weaknesses
depending on the structure of the data.
Single linkage (also called the nearest-neighbor method) defines the
distance between two clusters as the minimum distance between any pair of
points, one from each cluster. This method can detect and handle clusters of arbitrary
shape, including elongated and non-globular structures. However, it is
highly sensitive to noise and outliers, because a single pair of close
points is sufficient to merge two clusters.
Complete linkage defines
the distance between two clusters as the maximum distance between any pair
of points across the two clusters. It is less susceptible to noise and
outliers than single linkage. However, complete linkage is biased toward
producing globular clusters of similar size and may
incorrectly break apart large or irregularly shaped
clusters.
Formally, let <math>(S, \delta)</math> be a metric space, and let <math>A</math> and <math>B</math> be nonempty compact subsets of <math>S</math>. The distance from a point <math>a</math> to a set <math>B</math> is defined as:
:<math>\tilde{d}(a, B) = \inf_{b \in B} \delta(a, b)</math>
Extending this to sets, the directed distance from <math>A</math> to <math>B</math> is:
:<math>\tilde{d}(A, B) = \sup_{a \in A} \inf_{b \in B} \delta(a, b)</math>
Since this quantity is not symmetric, the Hausdorff distance between two sets is defined as:
:<math>d_H(A, B) = \max\{\tilde{d}(A, B), \tilde{d}(B, A)\}</math>
This definition captures the largest distance from a point in one set to the closest point in the other set. Equivalently, it can be interpreted as the smallest radius <math>r</math> such that every point of <math>A</math> lies within distance <math>r</math> of some point in <math>B</math>, and every point of <math>B</math> lies within distance <math>r</math> of some point in <math>A</math>. In this sense, the Hausdorff distance measures the maximum mismatch between two sets. Early merges at lower heights reflect the closest relationships between observations, while later merges combine increasingly different groups.
When the dendrogram is scaled using actual or normalized distances, the vertical spacing between merges provides insight into the relative differences between clusters. Large vertical gaps between successive merges often indicate meaningful separations in the data.
In practice, the selection of the number of clusters may also depend on domain knowledge or application-specific requirements. Quantitative measures such as the silhouette coefficient can be used to evaluate the quality of different clusterings. Because hierarchical clustering is sensitive to the choice of distance metric and linkage criterion, the optimal number of clusters may vary depending on these factors.
This method builds the hierarchy from the individual elements by progressively merging clusters. In our example, we have six elements {a} {b} {c} {d} {e} and {f}. The first step is to determine which elements to merge in a cluster. Usually, we want to take the two closest elements, according to the chosen distance.
Optionally, one can also construct a distance matrix at this stage, where the number in the i-th row j-th column is the distance between the i-th and j-th elements. Then, as clustering progresses, rows and columns are merged as the clusters are merged and the distances updated. This is a common way to implement this type of clustering, and has the benefit of caching distances between clusters. A simple agglomerative clustering algorithm is described in the single-linkage clustering page; it can easily be adapted to different types of linkage (see below).
Suppose we have merged the two closest elements b and c, we now have the following clusters {a}, {b, c}, {d}, {e} and {f}, and want to merge them further. To do that, we need to take the distance between {a} and {b c}, and therefore define the distance between two clusters.
Usually the distance between two clusters <math>\mathcal{A}</math> and <math>\mathcal{B}</math> is one of the following:
- The maximum distance between elements of each cluster (also called complete-linkage clustering):
::<math> \max \{\, d(x,y) : x \in \mathcal{A},\, y \in \mathcal{B}\,\}. </math>
- The minimum distance between elements of each cluster (also called single-linkage clustering):
::<math> \min \{\, d(x,y) : x \in \mathcal{A},\, y \in \mathcal{B} \,\}. </math>
- The mean distance between elements of each cluster (also called average linkage clustering, used e.g. in UPGMA):
::<math> {1 \over {|\mathcal{A}|\cdot|\mathcal{B}|\sum_{x \in \mathcal{A\sum_{ y \in \mathcal{B d(x,y). </math>
- The sum of all intra-cluster variance.
- The increase in variance for the cluster being merged (Ward's method
One can always decide to stop clustering when there is a sufficiently small number of clusters (number criterion). Some linkages may also guarantee that agglomeration occurs at a greater distance between clusters than the previous agglomeration, and then one can stop clustering when the clusters are too far apart to be merged (distance criterion). However, this is not the case of, e.g., the centroid linkage where the so-called reversals (inversions, departures from ultrametricity) may occur.
Divisive clustering
The basic principle of divisive clustering was published as the DIANA (DIvisive ANAlysis clustering) algorithm. Initially, all data is in the same cluster, and the largest cluster is split until every object is separate.
Because there exist <math>O(2^n)</math> ways of splitting each cluster, heuristics are needed. DIANA chooses the object with the maximum average dissimilarity and then moves all objects to this cluster that are more similar to the new cluster than to the remainder.
Informally, DIANA is not so much a process of "dividing" as it is of "hollowing out": each iteration, an existing cluster (e.g. the initial cluster of the entire dataset) is chosen to form a new cluster inside of it. Objects progressively move to this nested cluster, and hollow out the existing cluster. Eventually, all that's left inside a cluster is nested clusters that grew there, without it owning any loose objects by itself.
Formally, DIANA operates in the following steps:
- Let <math>C_0 = \{1\dots n\}</math> be the set of all <math>n</math> object indices and <math>\mathcal{C} = \{C_0\}</math> the set of all formed clusters so far.
- Iterate the following until <math>|\mathcal{C}| = n</math>:
- Find the current cluster with 2 or more objects that has the largest diameter: <math>C_* = \arg\max_{C\in \mathcal{C \max_{i_1,i_2\in C} \delta(i_1,i_2)</math>
- Find the object in this cluster with the most dissimilarity to the rest of the cluster: <math>i^* = \arg\max_{i\in C_*} \frac{1}{|C_*|-1}\sum_{j\in C_*\setminus\{i\ \delta(i,j)</math>
- Pop <math>i^*</math> from its old cluster <math>C_*</math> and put it into a new splinter group <math>C_\textrm{new} = \{i^*\}</math>.
- As long as <math>C_*</math> isn't empty, keep migrating objects from <math>C_*</math> to add them to <math>C_\textrm{new}</math>. To choose which objects to migrate, don't just consider dissimilarity to <math>C_*</math>, but also adjust for dissimilarity to the splinter group: let <math>i^* = \arg\max_{i\in C} D(i)</math> where we define <math>D(i) = \frac{1}{|C_*|-1}\sum_{j\in C_*\setminus\{i\ \delta(i,j) - \frac{1}{|C_\textrm{new}|}\sum_{j\in C_\textrm{new \delta(i,j)</math>, then either stop iterating when <math>D(i^*) < 0</math>, or migrate <math>i^*</math>.
- Add <math>C_\textrm{new}</math> to <math>\mathcal{C}</math>.
Intuitively, <math>D(i)</math> above measures how strongly an object wants to leave its current cluster, but it is attenuated when the object wouldn't fit in the splinter group either. Such objects will likely start their own splinter group eventually.
The dendrogram of DIANA can be constructed by letting the splinter group <math>C_\textrm{new}</math> be a child of the hollowed-out cluster <math>C_*</math> each time. This constructs a tree with <math>C_0</math> as its root and <math>n</math> unique single-object clusters as its leaves.
Software
Open source implementations
thumb|Hierarchical clustering [[dendrogram of the Iris dataset (using R). Source ]]
thumb|Hierarchical clustering and interactive dendrogram visualization in [[Orange (software)|Orange data mining suite.]]
- ALGLIB implements several hierarchical clustering algorithms (single-link, complete-link, Ward) in C++ and C# with O(n²) memory and O(n³) run time.
- ELKI includes multiple hierarchical clustering algorithms, various linkage strategies and also includes the efficient SLINK,
- Octave, the GNU analog to MATLAB implements hierarchical clustering in function "linkage".
- Orange, a data mining software suite, includes hierarchical clustering with interactive dendrogram visualisation.
- R has built-in functions and packages that provide functions for hierarchical clustering.
- SciPy implements hierarchical clustering in Python, including the efficient SLINK algorithm.
- scikit-learn also implements hierarchical clustering in Python.
- Weka includes hierarchical cluster analysis.
Commercial implementations
- MATLAB includes hierarchical cluster analysis.
- SAS includes hierarchical cluster analysis in PROC CLUSTER.
- Mathematica includes a Hierarchical Clustering Package.
- NCSS includes hierarchical cluster analysis.
- SPSS includes hierarchical cluster analysis.
- Qlucore Omics Explorer includes hierarchical cluster analysis.
- Stata includes hierarchical cluster analysis.
- CrimeStat includes a nearest neighbor hierarchical cluster algorithm with a graphical output for a Geographic Information System.
Scalable implementation with HAL-x
<!-- Ethan's rough draft for course project (Phase 2): tighten tone and verify all claims against sources. -->
Standard hierarchical agglomerative clustering is difficult to apply to very large datasets because typical implementations require substantial memory for pairwise structures and can scale poorly as <math>n</math> grows (see § Complexity above). In computational biology, single-cell technologies such as mass cytometry routinely produce tens of millions of high-dimensional observations, which motivates specialized workflows that preserve biological resolution without requiring that every expensive step be run on the full matrix at once.
Overview
HAL-x is a hierarchical density clustering algorithm introduced for large, high-dimensional point clouds. Rather than agglomerating solely from a fixed linkage criterion on a precomputed distance matrix, HAL-x builds an initial set of high-density “pure” clusters in a low-dimensional embedding of a downsampled subset of the data, then merges clusters using supervised linkage: it trains supervised classifiers on the original (unreduced) feature space to measure how reliably two proposed clusters can be separated, and uses those separation scores to decide merge order. The method outputs a hierarchy that can be read as a tree of merges together with a predictive model that can assign labels to held-out or newly collected cells without re-running the entire clustering pipeline on every point during exploratory iterations.
In outline, the published implementation proceeds by (i) normalizing and downsampling for dimensionality reduction (commonly t-SNE or UMAP), (ii) estimating density in the embedded space to obtain initial clusters, (iii) constructing a sparse k-nearest neighbor graph between clusters with edge weights derived from classifier-based separability on unreduced coordinates, and (iv) agglomeratively merging the weakest edges while retaining cross-validated accuracy information so users can obtain multiple resolutions from one fitted model.
