Hierarchical Clustering

May 21, 2023

Hierarchical Clustering is a technique used in unsupervised machine learning to group similar data points into clusters or sub-clusters. The algorithm creates a hierarchy of clusters by iteratively merging smaller clusters into larger ones based on a similarity measure. The resulting tree-like structure is called a dendrogram and can be used to visualize the relationships between data points.

Purpose and Usage

Hierarchical Clustering is commonly used in data analysis and pattern recognition for various applications such as gene expression analysis, image segmentation, market research, and social network analysis. It can also be used for anomaly detection, where outliers or anomalies are identified as separate clusters.

The algorithm is particularly useful when the number of clusters is unknown or when the data cannot be easily partitioned into distinct clusters. It provides a flexible and interpretable alternative to other clustering techniques such as K-means, which require the number of clusters to be specified in advance. Hierarchical Clustering can also be used in combination with other clustering techniques to refine or validate the results.

Brief History and Development

Hierarchical Clustering has a long history in the field of statistics and data analysis, dating back to the early 20th century. The method was originally developed for the analysis of gene expression data in the late 1960s, and since then, it has been widely used in various disciplines such as biology, psychology, and computer science.

The algorithm has undergone many improvements and variations over the years. One of the most significant developments was the introduction of agglomerative (bottom-up) clustering in the 1950s, which is now the most widely used method for hierarchical clustering. Another important development was the use of linkage criteria to measure the similarity between clusters, which has led to the development of various linkage methods such as single linkage, complete linkage, and average linkage.

Key Concepts and Principles

Distance Measures

The first step in Hierarchical Clustering is to define a distance measure between data points. The most commonly used distance measures are Euclidean distance and Manhattan distance, but other measures such as Pearson correlation and cosine similarity can also be used. The choice of distance measure depends on the nature of the data and the specific application.

Linkage Methods

The next step is to define a linkage method, which determines the way in which the similarity between clusters is calculated. The most commonly used linkage methods are:

  • Single Linkage: The similarity between two clusters is defined as the minimum distance between any two data points in the two clusters.
  • Complete Linkage: The similarity between two clusters is defined as the maximum distance between any two data points in the two clusters.
  • Average Linkage: The similarity between two clusters is defined as the average distance between all pairs of data points in the two clusters.

Other linkage methods such as Ward’s method and centroid linkage are also used in practice.


The output of Hierarchical Clustering is a dendrogram, which is a tree-like structure that shows the relationships between data points and clusters. The leaves of the tree represent the individual data points, and the branches represent the clusters at various levels of the hierarchy. The height of each branch represents the similarity between the clusters it connects.

Cutting the Dendrogram

To obtain a specific number of clusters, the dendrogram can be cut at a certain height, which corresponds to the desired number of clusters. The resulting clusters are the leaves of the tree below the cut, and the height of the cut corresponds to the similarity threshold used to define the clusters.

Pseudocode and Implementation Details

The pseudocode for Hierarchical Clustering is as follows:

Input: Data points D, Distance measure dist, Linkage method link
Output: Dendrogram T

1. Initialize each data point as a separate cluster
2. Compute the pairwise distances between all clusters
3. While there is more than one cluster:
      a. Compute the similarity between all pairs of clusters using the specified linkage method
      b. Merge the two most similar clusters into a single cluster
      c. Recompute the pairwise distances between the new cluster and all remaining clusters
      d. Update the dendrogram with the new cluster and the similarity between the merged clusters
4. Return the dendrogram T

The implementation of Hierarchical Clustering can be done using various programming languages such as Python, R, and MATLAB. There are also many libraries and packages available that provide pre-implemented functions for Hierarchical Clustering.

Examples and Use Cases

Gene Expression Analysis

Hierarchical Clustering is widely used in gene expression analysis to identify genes that are co-expressed under certain conditions or in specific tissues. The technique can help to identify functional gene modules and to understand the underlying regulatory mechanisms.

Image Segmentation

Hierarchical Clustering can be used for image segmentation by grouping similar pixels into regions or objects. The technique has been used in various applications such as object recognition, face detection, and texture analysis.

Market Research

Hierarchical Clustering can be used in market research to segment customers based on their behavior, preferences, and demographics. The technique can help to identify customer segments with distinct needs and to develop targeted marketing strategies.

Social Network Analysis

Hierarchical Clustering can be used in social network analysis to identify groups of individuals with similar attributes or behaviors. The technique can help to understand the structure and dynamics of social networks and to detect communities and sub-communities.

Advantages and Disadvantages


  • Hierarchical Clustering does not require the number of clusters to be specified in advance, making it a flexible and adaptable technique.
  • The resulting dendrogram provides a visual representation of the relationship between data points and clusters, which can aid in interpretation and understanding.
  • Hierarchical Clustering can handle different types of data and distance measures, making it a versatile technique that can be applied to various applications.
  • The algorithm can be used in combination with other clustering techniques to refine or validate the results.


  • Hierarchical Clustering can be computationally intensive for large datasets, especially when using distance measures that require a lot of computation.
  • The choice of distance measure and linkage method can have a significant impact on the results, and different choices may lead to different clustering outcomes.
  • The dendrogram can be difficult to interpret when the number of data points or clusters is large, and it may require additional analysis and visualization techniques to extract meaningful insights.

Divisive Clustering

Divisive Clustering is the opposite of Hierarchical Clustering, where instead of merging clusters, the algorithm recursively divides the data into smaller sub-groups based on a dissimilarity measure. The technique can be more computationally efficient than Hierarchical Clustering for large datasets, but it requires the number of clusters to be specified in advance.

Density-Based Clustering

Density-Based Clustering is a technique that identifies clusters based on regions of high density in the data space. The algorithm can handle clusters of different shapes and sizes and is robust to noise and outliers. The most commonly used density-based clustering technique is DBSCAN (Density-Based Spatial Clustering of Applications with Noise).