K-Means to Convergence
Description
Given a list of points and initial centroids, run Lloyd’s k-means to convergence: repeatedly assign each point to its nearest centroid by Euclidean distance (ties toward the lower index) and recompute each centroid as the mean of its assigned points, stopping when the assignments no longer change. A centroid with no points keeps its position. Return the final centroids, each coordinate rounded to 4 decimal places.
Examples
[[1,1],[2,2],[8,8],[9,9]], [[0,0],[10,10]][[1.5,1.5],[8.5,8.5]]Assignment and centroid updates alternate until membership stops changing, leaving each centroid at the mean of its final cluster.
[[0,0],[1,1],[10,10],[11,11]], [[0,0],[10,10]][[0.5,0.5],[10.5,10.5]]Assignment and centroid updates alternate until membership stops changing, leaving each centroid at the mean of its final cluster.
[[1,1],[2,2],[3,3]], [[1,1],[3,3]][[1.5,1.5],[3,3]]Assignment and centroid updates alternate until membership stops changing, leaving each centroid at the mean of its final cluster.
Constraints
- •
1 ≤ points, centroids ≤ 10³ - •
all vectors share the same dimension