A statistical method of analysis which seeks to build a hierarchy of clusters
"SLINK" redirects here. For the online magazine, see
Slink.
In data mining and statistics, hierarchical clustering (also called hierarchical cluster analysis or HCA) is a method of cluster analysis which seeks to build a hierarchy of clusters. Strategies for hierarchical clustering generally fall into two types:^{[1]}
 Agglomerative: This is a "bottomup" approach: each observation starts in its own cluster, and pairs of clusters are merged as one moves up the hierarchy.
 Divisive: This is a "topdown" approach: all observations start in one cluster, and splits are performed recursively as one moves down the hierarchy.
In general, the merges and splits are determined in a greedy manner. The results of hierarchical clustering^{[2]} are usually presented in a dendrogram.
The standard algorithm for hierarchical agglomerative clustering (HAC) has a time complexity of ${\mathcal {O}}(n^{3})$ and requires $\Omega (n^{2})$ memory, which makes it too slow for even medium data sets. However, for some special cases, optimal efficient agglomerative methods (of complexity ${\mathcal {O}}(n^{2})$) are known: SLINK^{[3]} for singlelinkage and CLINK^{[4]} for completelinkage clustering. With a heap, the runtime of the general case can be reduced to ${\mathcal {O}}(n^{2}\log n)$, an improvement on the aforementioned bound of ${\mathcal {O}}(n^{3})$, 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.
Except for the special case of singlelinkage, none of the algorithms (except exhaustive search in ${\mathcal {O}}(2^{n})$) can be guaranteed to find the optimum solution.
Divisive clustering with an exhaustive search is ${\mathcal {O}}(2^{n})$, but it is common to use faster heuristics to choose splits, such as kmeans.
Cluster dissimilarity
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 metric (a measure of distance between pairs of observations), and a linkage criterion which specifies the dissimilarity of sets as a function of the pairwise distances of observations in the sets.
Metric
The choice of an appropriate metric will influence the shape of the clusters, as some elements may be relatively closer to one another under one metric than another. For example, in two dimensions, under the Manhattan distance metric, the distance between the origin (0,0) and (0.5, 0.5) is the same as the distance between the origin and (0, 1), while under the Euclidean distance metric the latter is strictly greater.
Some commonly used metrics for hierarchical clustering are:^{[5]}
For text or other nonnumeric data, metrics such as the Hamming distance or Levenshtein distance are often used.
A review of cluster analysis in health psychology research found that the most common distance measure in published studies in that research area is the Euclidean distance or the squared Euclidean distance.^{[citation needed]}
Linkage criteria
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 are:^{[6]}^{[7]}
Names

Formula

Maximum or completelinkage clustering

$\max \,\{\,d(a,b):a\in A,\,b\in B\,\}.$

Minimum or singlelinkage clustering

$\min \,\{\,d(a,b):a\in A,\,b\in B\,\}.$

Unweighted average linkage clustering (or UPGMA)

${\frac {1}{A\cdot B}}\sum _{a\in A}\sum _{b\in B}d(a,b).$

Weighted average linkage clustering (or WPGMA)

$d(i\cup j,k)={\frac {d(i,k)+d(j,k)}{2}}.$

Centroid linkage clustering, or UPGMC

$\c_{s}c_{t}\$ where $c_{s}$ and $c_{t}$ are the centroids of clusters s and t, respectively.

Minimum energy clustering

${\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}$

where d is the chosen metric. Other linkage criteria include:
 The sum of all intracluster variance.
 The increase in variance for the cluster being merged (Ward's criterion).^{[8]}
 The probability that candidate clusters spawn from the same distribution function (Vlinkage).
 The product of indegree and outdegree on a knearestneighbour graph (graph degree linkage).^{[9]}
 The increment of some cluster descriptor (i.e., a quantity defined for measuring the quality of a cluster) after merging two clusters.^{[10]}^{[11]}^{[12]}
Discussion
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.
Agglomerative clustering example
Raw data
For example, suppose this data is to be clustered, and the Euclidean distance is the distance metric.
The hierarchical clustering dendrogram would be as such:
Traditional representation
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 ith row jth column is the distance between the ith and jth 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 singlelinkage 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 ${\mathcal {A}}$ and ${\mathcal {B}}$ is one of the following:
 $\max\{\,d(x,y):x\in {\mathcal {A}},\,y\in {\mathcal {B}}\,\}.$
 $\min\{\,d(x,y):x\in {\mathcal {A}},\,y\in {\mathcal {B}}\,\}.$
 The mean distance between elements of each cluster (also called average linkage clustering, used e.g. in UPGMA):
 ${1 \over {{\mathcal {A}}\cdot {\mathcal {B}}}}\sum _{x\in {\mathcal {A}}}\sum _{y\in {\mathcal {B}}}d(x,y).$
 The sum of all intracluster variance.
 The increase in variance for the cluster being merged (Ward's method^{[8]})
 The probability that candidate clusters spawn from the same distribution function (Vlinkage).
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.^{[13]}
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 socalled reversals^{[14]} (inversions, departures from ultrametricity) may occur.
Divisive clustering
The basic principle of divisive clustering was published as the DIANA (DIvisive ANAlysis Clustering) algorithm.^{[15]} Initially, all data is in the same cluster, and the largest cluster is split until every object is separate.
Because there exist $O(2^{n})$ 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.
Software
Open source implementations
 ALGLIB implements several hierarchical clustering algorithms (singlelink, completelink, 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,^{[3]} CLINK^{[4]} and Anderberg algorithms, flexible cluster extraction from dendrograms and various other cluster analysis algorithms.
 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 many packages that provide functions for hierarchical clustering.
 SciPy implements hierarchical clustering in Python, including the efficient SLINK algorithm.
 scikitlearn 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.
See also
References
 ^ Rokach, Lior, and Oded Maimon. "Clustering methods." Data mining and knowledge discovery handbook. Springer US, 2005. 321352.
 ^ Frank Nielsen (2016). "Chapter 8: Hierarchical Clustering". Introduction to HPC with MPI for Data Science. Springer.
 ^ ^{a} ^{b} R. Sibson (1973). "SLINK: an optimally efficient algorithm for the singlelink cluster method" (PDF). The Computer Journal. British Computer Society. 16 (1): 30–34. doi:10.1093/comjnl/16.1.30.
 ^ ^{a} ^{b} D. Defays (1977). "An efficient algorithm for a completelink method". The Computer Journal. British Computer Society. 20 (4): 364–366. doi:10.1093/comjnl/20.4.364.
 ^ "The DISTANCE Procedure: Proximity Measures". SAS/STAT 9.2 Users Guide. SAS Institute. Retrieved 20090426.
 ^ "The CLUSTER Procedure: Clustering Methods". SAS/STAT 9.2 Users Guide. SAS Institute. Retrieved 20090426.
 ^ Székely, G. J.; Rizzo, M. L. (2005). "Hierarchical clustering via Joint BetweenWithin Distances: Extending Ward's Minimum Variance Method". Journal of Classification. 22 (2): 151–183. doi:10.1007/s0035700500129.
 ^ ^{a} ^{b} Ward, Joe H. (1963). "Hierarchical Grouping to Optimize an Objective Function". Journal of the American Statistical Association. 58 (301): 236–244. doi:10.2307/2282967. JSTOR 2282967. MR 0148188.
 ^ Zhang, Wei; Wang, Xiaogang; Zhao, Deli; Tang, Xiaoou (2012). Fitzgibbon, Andrew; Lazebnik, Svetlana; Perona, Pietro; Sato, Yoichi; Schmid, Cordelia (eds.). "Graph Degree Linkage: Agglomerative Clustering on a Directed Graph". Computer Vision – ECCV 2012. Lecture Notes in Computer Science. Springer Berlin Heidelberg. 7572: 428–441. arXiv:1208.5092. Bibcode:2012arXiv1208.5092Z. doi:10.1007/9783642337185_31. ISBN 9783642337185. See also: https://github.com/waynezhanghk/gacluster
 ^ Zhang, et al. "Agglomerative clustering via maximum incremental path integral." Pattern Recognition (2013).
 ^ Zhao, and Tang. "Cyclizing clusters via zeta function of a graph."Advances in Neural Information Processing Systems. 2008.
 ^ Ma, et al. "Segmentation of multivariate mixed data via lossy data coding and compression." IEEE Transactions on Pattern Analysis and Machine Intelligence, 29(9) (2007): 15461562.
 ^ Fernández, Alberto; Gómez, Sergio (2008). "Solving Nonuniqueness in Agglomerative Hierarchical Clustering Using Multidendrograms". Journal of Classification. 25 (1): 43–65. arXiv:cs/0608049. doi:10.1007/s003570089004x.
 ^ Legendre, P.; Legendre, L. (2003). Numerical Ecology. Elsevier Science BV.
 ^ Kaufman, L., & Rousseeuw, P. J. (1990). Finding Groups in Data  An Introduction to Cluster Analysis. A WileyScience Publication John Wiley & Sons.