Implement K-means
Asked in the Online assessment stage. ML algorithm from scratch.
Given points, a list of d-dimensional numeric points, an integer k, and initial centroids, repeatedly assign each point to its nearest centroid and recompute each centroid as the coordinate-wise mean of its assigned points. Use squared Euclidean distance, break ties by choosing the lowest centroid index, and leave an empty cluster's centroid unchanged. Stop when centroids no longer change or max_iterations is reached.
Implement def k_means(points, k, initial_centroids, max_iterations):. Return [centroids, labels], where centroids is a list of k points and labels[i] is the assigned centroid index for points[i].
def k_means(points, k, initial_centroids, max_iterations):