devnotes.

Hierarchical Clustering

Hierarchical clustering builds a full tree of nested groupings rather than a single fixed set of k clusters — useful when the natural number of groups isn't known ahead of time, or when the relationships between clusters themselves are meaningful.

from scipy.cluster.hierarchy import dendrogram, linkage
from sklearn.cluster import AgglomerativeClustering
import matplotlib.pyplot as plt

linked = linkage(X_scaled, method="ward")   # computes the full merge hierarchy
dendrogram(linked)
plt.title("Hierarchical Clustering Dendrogram")
plt.show()

model = AgglomerativeClustering(n_clusters=3)   # cut the tree to get a specific number of clusters
clusters = model.fit_predict(X_scaled)

Agglomerative (bottom-up) hierarchical clustering starts with every point as its own individual cluster, then repeatedly merges the two closest clusters together, one merge at a time, until everything collapses into a single cluster — the full sequence of merges is visualized as a dendrogram, a tree diagram where each merge is shown at the height representing how dissimilar the merged clusters were.

Unlike k-means, hierarchical clustering doesn't require choosing k in advance — you can inspect the dendrogram after the fact and decide how many clusters make sense by choosing where to "cut" the tree horizontally, with the number of vertical lines crossed at that height indicating the resulting cluster count. This makes it particularly useful for exploratory analysis where the "right" number of groups genuinely isn't known, and the dendrogram itself often reveals meaningful sub-structure (clusters within clusters) that a flat k-means result can't show. The tradeoff is computational cost — hierarchical clustering scales considerably worse than k-means on large datasets, making it best suited to small-to-medium-sized data rather than very large ones.

Hierarchical Clustering and Dendrograms | DevNotes