Unlike K-Means, which requires pre-specifying the number of clusters K and partitions data into flat buckets, Hierarchical Clustering builds a multi-level nested tree of clusters called a Dendrogram. This allows researchers and data scientists to explore relationships at multiple granularities simultaneously.
N clusters). At each iteration, the two most similar clusters are merged together, until all points coalesce into a single root cluster.When deciding which two multi-point clusters to merge, the chosen Linkage Criterion dramatically shapes the resulting tree geometry:
| Linkage Method | Mathematical Rule | Properties & Typical Cluster Shape |
|---|---|---|
| Ward Linkage (Default) | Minimizes total within-cluster variance increase after merging. | Produces compact, spherical, equal-sized clusters. Highly robust. |
| Complete (Maximum) | Maximum distance between any sample in A and any sample in B. | Finds tight, compact clusters; sensitive to outliers. |
| Single (Minimum) | Minimum distance between any sample in A and any sample in B. | Can trace elongated chains, but suffers from "chaining effect" noise. |
| Average | Average distance between all pairwise samples across A and B. | Balanced compromise between Single and Complete linkage. |
A Dendrogram visualizes the hierarchical merger sequence. The vertical height of each horizontal cross-bar represents the exact distance threshold at which two clusters were merged. To choose the number of clusters, locate the longest vertical lines that are not intercepted by any horizontal bar, and draw a horizontal threshold cut line across the dendrogram.
from scipy.cluster.hierarchy import dendrogram, linkage
from sklearn.cluster import AgglomerativeClustering
import matplotlib.pyplot as plt
# Generate linkage matrix using Ward criterion
Z = linkage(X, method='ward')
# In Scikit-Learn: Fit Agglomerative model directly
agg = AgglomerativeClustering(n_clusters=3, linkage='ward')
cluster_labels = agg.fit_predict(X)
Broaden your foundational and advanced Machine Learning knowledge across our curriculum:
Contrast agglomerative bottom-up clustering with centroid partitions.
Overcome linkage sensitivity with density-based grouping.
Visualize hierarchical tree relationships in non-linear embeddings.
Dendrogram cutting rules and cophenetic correlation.