Implement k-means clustering.
Asked in the PyTorch coding stage. This was the second problem in the PyTorch coding round. Use deterministic initialization: divide the input points into k consecutive balanced groups and initialize each centroid as that group's mean.
Implement k_means(points, k, max_iterations). points is a nonempty list of equal-length numeric lists. Return (centroids, labels), where centroids is a list of k coordinate lists and labels[i] is the index of the nearest centroid for points[i]. Use squared Euclidean distance, preserve the lowest-index centroid on ties, and leave an empty cluster's centroid unchanged.
def k_means(points, k, max_iterations):