k-means represents each cluster by its mean, and a single outlier can drag a mean far away. k-medoids represents each cluster by one of its actual data points, the medoid that minimizes total distance to the other members. Because the center is always a real point, it is less affected by outliers, and it works with any distance function.
Implement k_medoids(X, k, max_iter=100, seed=0), which returns (labels, medoid_indices). Use Euclidean distance.
np.random.default_rng(seed).max_iter is reached.1 <= k <= n, otherwise raise ValueError.labels[medoid_indices[c]] == c for every cluster c.Open one at a time. Each gives away a little more than the last.
Compute the full pairwise distance matrix once. Then assignment is argmin over the medoid columns.
Inside a cluster, use D[np.ix_(members, members)].sum(axis=1). Its argmin gives the new medoid position.
No results yet
Run your code to check it against the examples, or submit to run every test.