Sign in to track progress, unlock prep layers, and browse your pipeline.Sign in
Implement K-means clustering from scratch
Task
Implement the K-means clustering algorithm from scratch, without using any machine learning libraries.
You will be provided with:
- An input dataset
X: an array of shape(n, d), representingndata points, each withddimensions. - An integer
k: the desired number of clusters. max_iters: the maximum number of iterations to run the algorithm.tol: a convergence tolerance threshold.
Requirements
- Initialization: Initialize
kcentroids. A standard approach, such as randomly selectingkpoints from the input datasetX, is acceptable. - Iterative Process: Repeat the following steps until either convergence is achieved (based on
tol) ormax_itersis reached:- Assignment Step: Assign each data point to its nearest centroid. The Euclidean distance should be used as the distance metric.
- Update Step: Recompute each centroid as the mean of all data points currently assigned to it.
- Edge Case Handling: Properly handle the scenario where a cluster becomes empty during the update step.
Output
The function should return:
centroids: an array of shape(k, d), representing the final positions of thekcluster centroids.labels: an array of lengthn, wherelabels[i]is the index of the cluster (from0tok-1) to which thei-th data point belongs.
Follow-ups (Discussion)
- Analyze the time complexity of the algorithm per iteration.
- Discuss how the centroid initialization strategy impacts the algorithm's convergence properties and the quality of the resulting clusters. Specifically, consider the idea behind K-means++.
AI interview prep
Core dilemma
Balancing computational efficiency for potentially large datasets with the robustness and correctness of an iterative algorithm, especially concerning convergence, initialization, and edge cases like empty clusters.
Mid-level signals3
Senior signals3
Red flags3
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement K-means clustering from scratch
Task
Implement the K-means clustering algorithm from scratch, without using any machine learning libraries.
You will be provided with:
- An input dataset
X: an array of shape(n, d), representingndata points, each withddimensions. - An integer
k: the desired number of clusters. max_iters: the maximum number of iterations to run the algorithm.tol: a convergence tolerance threshold.
Requirements
- Initialization: Initialize
kcentroids. A standard approach, such as randomly selectingkpoints from the input datasetX, is acceptable. - Iterative Process: Repeat the following steps until either convergence is achieved (based on
tol) ormax_itersis reached:- Assignment Step: Assign each data point to its nearest centroid. The Euclidean distance should be used as the distance metric.
- Update Step: Recompute each centroid as the mean of all data points currently assigned to it.
- Edge Case Handling: Properly handle the scenario where a cluster becomes empty during the update step.
Output
The function should return:
centroids: an array of shape(k, d), representing the final positions of thekcluster centroids.labels: an array of lengthn, wherelabels[i]is the index of the cluster (from0tok-1) to which thei-th data point belongs.
Follow-ups (Discussion)
- Analyze the time complexity of the algorithm per iteration.
- Discuss how the centroid initialization strategy impacts the algorithm's convergence properties and the quality of the resulting clusters. Specifically, consider the idea behind K-means++.