Hierarchical Agglomerative Clustering (HAC) is a popular algorithm used in unsupervised machine learning and data analysis. Its primary goal is to group similar objects into groups called clusters. Unlike other clustering methods that require a predefined number of clusters, HAC builds a hierarchy of clusters, offering a multi-level view of the data structure. This makes it particularly useful for exploratory data analysis where the relationships between data points are not immediately obvious.
The "Hierarchical" part of the name refers to the tree-like structure created during the process, while "Agglomerative" describes the specific approach taken: a bottom-up strategy. The algorithm starts by treating every single data point as its own individual cluster. As the process progresses, it iteratively merges the closest pairs of clusters until only one single cluster remains containing all the data points.
This process creates a nested sequence of clusterings. At the bottom level, where many small clusters exist, the data is highly specific andgranular. As we move up the hierarchy, clusters become larger and more generalized, encompassing more data points but potentially losing fine-grained details.
The execution of Hierarchical Agglomerative Clustering can be broken down into a systematic series of steps:
One of the most critical decisions when implementing HAC is choosing how to measure the distance between two clusters. When clusters contain multiple points, defining "distance" becomes ambiguous. This is where linkage methods come in. The choice of linkage significantly impacts the shape and nature of the final clusters.
1. Single Linkage: The distance between two clusters is defined as the shortest distance between any single member of the first cluster and any single member of the second cluster. This method tends to produce long, thin clusters or "chains." It can be sensitive to noise and outliers, as a single bridging point can merge two otherwise distinct clusters.
2. Complete Linkage: Also known as Maximum Linkage, this defines the distance between two clusters as the longest distance between any member of the first cluster and any member of the second. This approach favors compact, spherical clusters. However, it can be sensitive to outliers, as a single distant point can prevent the merging of clusters that should otherwise be grouped together.
3. Average Linkage: This method takes the average of all pairwise distances between the members of the two clusters. It is often considered a compromise between Single and Complete linkage. It tends to produce more balanced, spherical clusters and is generally more robust to noise than Single linkage.
4. Wards Linkage: This method minimizes the variance within each cluster. Instead of simply measuring distance, it calculates the increase in the total sum of squared errors when two clusters are merged. It is widely popular because it tends to produce clusters of relatively equal size and is very effective for many real-world datasets.
The output of HAC is typically visualized using a dendrogram. A dendrogram is a tree-like diagram that records the sequence of merges or splits.
In a dendrogram, the x-axis represents the data points (or original observations), and the y-axis represents the distance or dissimilarity at which the clusters were merged. The height of the vertical lines connecting clusters indicates the distance metric used for the merge. Therefore, the higher the vertical line, the more dissimilar the two clusters were when they were joined.
To determine the number of clusters for your final model, you draw a horizontal line across the dendrogram at a specific height (threshold). The number of vertical lines this horizontal line intersects is the number of clusters you keep. By adjusting the height of this cut, you can choose a granularity that fits your needs.
Like any machine learning algorithm, Hierarchical Agglomerative Clustering has its strengths and weaknesses.
Advantages:
Disadvantages:
Hierarchical Agglomerative Clustering is a powerful tool for understanding the inherent structure of data. It excels in scenarios where the relationship between data points is hierarchical or when preserving the history of how clusters merge is valuable. While it may not be the best choice for massive datasets due to computational constraints, its detailed visual output via dendrograms and lack of requirement for defining cluster numbers upfront make it an indispensable technique in the toolbox of data scientists.
