Premium problem129. One Lloyd Step of k-Means

Medium Locked

Given points X (n, d) and centroids C (k, d), return (labels, new_centroids):

  • labels (n,) assigns each point to its nearest centroid by squared Euclidean distance, with ties going to the lowest index
  • new_centroids (k, d) is the mean of each cluster
  • an empty cluster keeps its previous centroid

Input

X =
[[ 0.  0.]
 [ 0.  1.]
 [10. 10.]]
C =
[[0. 0.]
 [9. 9.]]

Output

(array([0, 0, 1]), array([[ 0. ,  0.5],
       [10. , 10. ]]))

Premium problem

This one's part of Premium. Unlock the full NumPy track plus every other premium problem on the site.

Implement solve(...)