Write a k-means algorithm in Python.
Asked in the coding round 2 stage. This is an ML-from-scratch coding task.
Implement k_means(points, k, initial_centroids, max_iterations). Each point and centroid is a numeric list with the same dimension. Return the final centroids only. Assign each point to the nearest centroid using squared Euclidean distance, breaking ties by choosing the lowest centroid index. Recompute each centroid as the coordinate-wise mean of its assigned points. If a cluster is empty, retain its previous centroid. Stop early when centroids no longer change.
Example: points = [[0, 0], [0, 2], [10, 10], [10, 12]], initial_centroids = [[0, 0], [10, 10]] returns [[0.0, 1.0], [10.0, 11.0]].
Constraints: 1 <= len(points), 1 <= k <= len(points), 1 <= len(point) <= 20, and 1 <= max_iterations <= 100.
def k_means(points, k, initial_centroids, max_iterations):