K-Means, GMM, and DBSCAN each build clusters by some form of "which points does this cluster currently claim," starting from some notion of cluster centers or dense regions. Agglomerative (bottom-up) hierarchical clustering takes the opposite approach entirely: start with every point as its own cluster, and repeatedly merge whichever two clusters are closest, until only the desired number remain.
Implement cluster_distance(points_a, points_b, linkage) and agglomerative_fit(input, n_clusters, linkage="single"). linkage is 'single', 'complete', or 'average' — anything else raises ValueError. Reuse 01-knn's pairwise_distances.
cluster_distance computes the distance between two groups of points under the given linkage: 'single' (closest pair), 'complete' (farthest pair), 'average' (mean over all pairs).linkage raises ValueError.agglomerative_fit starts with every row as its own cluster and repeatedly merges the two closest (by cluster_distance) until exactly n_clusters remain.Open one at a time. Each gives away a little more than the last.
cluster_distance is one full pairwise distance matrix between the two groups, reduced by .min(), .max(), or .mean() depending on linkage — no need to loop over pairs by hand.
Represent clusters as a plain Python list of index lists, not a NumPy array — the number of clusters shrinks every iteration, and each cluster's member count varies, which a ragged list handles naturally.
No results yet
Run your code to check it against the examples, or submit to run every test.