Before you start
Implement Lloyd's algorithm, the standard k-means loop.
Starting from initial_centroids of shape (k, d), repeat iters times:
Return (centroids, labels) where centroids has shape (k, d) and labels
has shape (n,). The labels must be the assignment computed against the
returned centroids, so run one final assignment step after the last update.
Real k-means is randomised through its initialisation. Taking the centroids as an argument makes the result deterministic and testable, and it is also how k-means++ plugs in.
def kmeans(X, initial_centroids, iters):
Input
X = np.array([[0.0], [0.5], [10.0], [10.5]])
kmeans(X, np.array([[0.0], [1.0]]), iters=5)
Output
(array([[ 0.25], [10.25]]), array([0, 0, 1, 1]))
Reasoning
In the first pass, 0.5 is equally far from both centroids, so it goes to the lower index, 0. The large points go to centroid 1. The centroids move to 0.25 and 10.25, and after that nothing changes.
Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell