Your question is Implement an ML Algorithm in Python. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Mastech Digital's delivery analytics pipeline needs a dependency-free implementation of K-means clustering for grouping multidimensional service metrics. Implement the clustering algorithm without using machine learning libraries.
Given n points in d-dimensional space, divide them into exactly k clusters. Use deterministic farthest-point initialization: choose the first input point as the first centroid, then repeatedly choose the point whose distance to its nearest selected centroid is greatest. Break all distance ties by choosing the lowest input index.
Repeat these steps until convergence or max_iter iterations:
Return [labels, centroids], where labels[i] is the cluster index for points[i], and centroids is a list of k coordinate lists. Stop when every centroid moves by at most tol in Euclidean distance.
def k_means(points, k, max_iter, tol):