Hierarchical clustering

Revision as of 11:28, 23 May 2025 by 240b:10:2760:7810:40b2:7c79:146e:286b (talk)
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

Template:Short description Template:Redirect Template:Machine learning

In data mining and statistics, hierarchical clustering<ref name="HC">Template:Cite book</ref> (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: 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)<ref name=":4">Template:Cite journal</ref>. 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 <ref>Template:Cite journal</ref>.
  • 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<ref name="HC" /> are usually presented in a dendrogram.

Hierarchical clustering has the distinct advantage that any valid measure of distance can be used. In fact, the observations themselves are not required: all that is used is a matrix of distances. On the other hand, except for the special case of single-linkage distance, none of the algorithms (except exhaustive search in <math>\mathcal{O}(2^n)</math>) can be guaranteed to find the optimum solution.Template:Cn

ComplexityEdit

The standard algorithm for hierarchical agglomerative clustering (HAC) has a time complexity of <math>\mathcal{O}(n^3)</math> and requires <math>\Omega(n^2)</math> memory, which makes it too slow for even medium data sets. However, for some special cases, optimal efficient agglomerative methods (of complexity <math>\mathcal{O}(n^2)</math>) are known: SLINK<ref name="SLINK">Template:Cite journal</ref> for single-linkage and CLINK<ref name="CLINK">Template:Cite journal</ref> 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>, an improvement on the aforementioned bound of <math>\mathcal{O}(n^3)</math>, at the cost of further increasing 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.<ref name=DE>Template:Cite journal</ref>

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.

Cluster LinkageEdit

In order to decide which clusters should be combined (for agglomerative), or where a cluster should be split (for divisive), a measure of dissimilarity between sets of observations is required. In most methods of hierarchical clustering, this is achieved by use of an appropriate distance d, such as the Euclidean distance, between single observations of the data set, and a linkage criterion, which specifies the dissimilarity of sets as a function of the pairwise distances of observations in the sets. The choice of metric as well as linkage can have a major impact on the result of the clustering, where the lower level metric determines which objects are most similar, whereas the linkage criterion influences the shape of the clusters <ref name=":5" />. For example, complete-linkage tends to produce more spherical clusters than single-linkage.

The linkage criterion determines the distance between sets of observations as a function of the pairwise distances between observations.

Some commonly used linkage criteria between two sets of observations A and B and a distance d are:<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref><ref>Template:Cite journal</ref>

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) 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<ref name=":6">Template:Cite journal</ref> A|\cdot|B|} \sum_{a \in A }\sum_{ b \in B} d(a,b)^p}, p\neq 0</math>
Ward linkage,<ref name="wards method">Template:Cite journal</ref> Minimum Increase of Sum of Squares (MISSQ)<ref name=":0">Template:Citation</ref> 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)<ref name=":0" /> <math>\sum_{x\in A\cup B} \lVert x - \mu_{A\cup B} \rVert^2</math>
Minimum Increase in Variance (MIVAR)<ref name=":0" /> A\cup B|}\sum_{x\in A\cup B} \lVert x - \mu_{A\cup B} \rVert^2

- \frac{1}{|A|}\sum_{x\in A} \lVert x - \mu_{A} \rVert^2 - \frac{1}{|B|}\sum_{x\in B} \lVert x - \mu_{B} \rVert^2</math><math>= \text{Var}(A\cup B) - \text{Var}(A) - \text{Var}(B)</math>

Minimum Variance (MNVAR)<ref name=":0" /> A\cup B|}\sum_{x\in A\cup B} \lVert x - \mu_{A\cup B} \rVert^2 = \text{Var}(A\cup B)</math>
Hausdorff linkage<ref>Template:Cite journal</ref> <math>\max_{x\in A\cup B} \min_{y\in A\cup B} d(x,y)</math>
Minimum Sum Medoid linkage<ref name=":1">Template:Cite conference</ref> <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<ref name=":1" /> <math>\min_{m\in A\cup B} \sum_{y\in A\cup B} d(m,y)

- \min_{m\in A} \sum_{y\in A} d(m,y) - \min_{m\in B} \sum_{y\in B} d(m,y)</math>

Medoid linkage<ref>Template:Cite conference</ref><ref>Template:Cite conference</ref> <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 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:

Agglomerative clustering exampleEdit

For example, suppose this data is to be clustered, and the Euclidean distance is the distance metric.

The hierarchical clustering dendrogram would be:

Cutting the tree at a given height will give a partitioning clustering at a selected precision. In this example, cutting after the second row (from the top) of the dendrogram will yield clusters {a} {b c} {d e} {f}. Cutting after the third row will yield clusters {a} {b c} {d e f}, which is a coarser clustering, with a smaller number but larger clusters.

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:

<math> \max \{\, d(x,y) : x \in \mathcal{A},\, y \in \mathcal{B}\,\}. </math>
<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<ref name="wards method"/>)
  • The probability that candidate clusters spawn from the same distribution function (V-linkage).

In case of tied minimum distances, a pair is randomly chosen, thus being able to generate several structurally different dendrograms. Alternatively, all tied pairs may be joined at the same time, generating a unique dendrogram.<ref>Template:Cite journal</ref>

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<ref>Template:Cite book</ref> (inversions, departures from ultrametricity) may occur.

Divisive clusteringEdit

The basic principle of divisive clustering was published as the DIANA (DIvisive ANAlysis clustering) algorithm.<ref>Template:Cite book</ref> 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:

  1. 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.
  2. Iterate the following until <math>|\mathcal{C}| = n</math>:
    1. 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>
    2. 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>
    3. 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>.
    4. 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>.
    5. 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.

Greedy Nature of the AlgorithmEdit

Hierarchical clustering is often described as a greedy algorithm because it makes a series of locally optimal choices without reconsidering previous steps. At each iteration, it merges the two clusters that are closest together based on a selected distance metric, always choosing the best immediate option available. This approach is "greedy" because it seeks to optimize the current decision rather than planning for the best possible overall clustering <ref name=":4" />. Once two clusters are merged, the decision is final and irreversible, without the possibility of backtracking, which can lead to suboptimal results if earlier choices were not ideal. Despite this, the greedy nature of hierarchical clustering makes it computationally efficient and simple to implement, though it may not always capture the true underlying structure of complex datasets <ref name=":5" />.

LimitationsEdit

Hierarchical clustering, particularly in its standard agglomerative form, presents several notable limitations: (a) Time Complexity: Hierarchical clustering, especially in its basic agglomerative form, has a high time complexity of  O(n³). This becomes a significant bottleneck for large datasets, limiting its scalability <ref name="CLINK2">Template:Cite journal</ref>. (b) Scalability: Due to the time and space complexity, hierarchical clustering algorithms struggle to handle very large datasets efficiently <ref name=DE/>  (c) Sensitivity to Noise and Outliers: Hierarchical clustering methods can be sensitive to noise and outliers in the data, which can lead to the formation of inaccurate or misleading cluster hierarchies <ref name="SLINK2">Template:Cite journal</ref>. (d) Difficulty with High-Dimensional Data: In high-dimensional spaces, hierarchical clustering can face challenges due to the curse of dimensionality, where data points become sparse, and distance measures become less meaningful. This can result in poorly defined clusters<ref name=":6" /><ref name=":3">Template:Cite conference</ref>. (e) Inability to Handle Non-Convex Shapes and Varying Densities: Traditional hierarchical clustering methods, like many other clustering algorithms, often assume that clusters are convex and have similar densities. They may struggle to accurately identify clusters with non-convex shapes or varying densities <ref name=":5">Template:Cite journal</ref>.

SoftwareEdit

Open source implementationsEdit

File:Iris dendrogram.png
Hierarchical clustering dendrogram of the Iris dataset (using R). Source
File:Orange-data-mining-hierarchical-clustering.png
Hierarchical clustering and interactive dendrogram visualization in 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,<ref name="SLINK" /> CLINK<ref name="CLINK" /> and Anderberg algorithms, flexible cluster extraction from dendrograms and various other cluster analysis algorithms.
  • Julia has an implementation inside the Clustering.jl package.<ref>{{#invoke:citation/CS1|citation

|CitationClass=web }}</ref>

  • 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<ref>{{#invoke:citation/CS1|citation

|CitationClass=web }}</ref> and packages that provide functions for hierarchical clustering.<ref>Template:Citation</ref><ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref><ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

  • 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 implementationsEdit

  • 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.

See alsoEdit

Template:Div col

ReferencesEdit

Template:Reflist

Further readingEdit

Template:Authority control