Hierarchical Agglomerative Clustering

medium
Unit 8 · Clustering0/3 to milestone

Agglomerative clustering starts with every point in its own cluster and repeatedly merges the two closest clusters. The linkage defines the distance between two clusters:

  • "single": the closest pair of points (finds long chains)
  • "complete": the farthest pair of points (finds compact blobs)
  • "average": the mean distance over all pairs

Implement agglomerative(X, n_clusters, linkage="single") with Euclidean distances, returning integer labels. Keep the clusters in a list ordered by their smallest member index. At each step, merge the closest pair; break ties by choosing the pair (i, j) that comes first in that list order. Number the final clusters 0, 1, … in order of their smallest member.

Also return the merge history: implement merge_distances(X, linkage), the list of linkage distances of the n − 1 merges, in order.

#clustering#unsupervised#linkage2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Hierarchical Agglomerative Clustering · TechGradify