Step by Step
K
Choose K — the number of clusters
Before running the algorithm, you must decide K, the number of clusters to look for. This is the central decision the elbow method helps you make.
Example: choosing K=4 to segment customers into four behavioral groups.
M
Move centroids — recalculate cluster centers
After points are assigned to clusters, each centroid is recalculated as the mean position of all points currently assigned to it.
Example: if five customers are assigned to cluster 2, that cluster's new centroid becomes the average of those five customers' feature values.
A
Assign nearest — group points to their closest centroid
Every data point is assigned to whichever centroid is currently closest to it, typically measured with Euclidean distance.
Example: a customer's data point gets assigned to cluster 3 because centroid 3 is currently the nearest of the K centroids.
S
Repeat until stable — converge on final clusters
The assign-and-move cycle repeats until the centroids stop moving significantly, meaning the clusters have stabilized.
Example: after 12 rounds of reassigning points and recalculating centroids, the clusters stop changing — the algorithm has converged.
Applied Walkthrough
1
A data scientist needs to choose K for a customer segmentation project but has no prior knowledge of how many segments naturally exist.
2
They plot inertia (a measure of how tightly clustered points are) against different values of K.
3
As K increases, inertia keeps decreasing, but at some point the rate of improvement slows sharply, creating a visible "elbow" bend in the plot.
4
That elbow point is chosen as the optimal K — beyond it, adding more clusters gives diminishing returns for the added complexity.
Exam Application
Exams test whether you understand the elbow method's purpose (choosing K objectively rather than arbitrarily) and whether you know the alternative: DBSCAN, which finds arbitrarily shaped clusters and automatically detects outliers without requiring you to specify K in advance at all.
⚠ Common Trap
The most common trap is assuming K-Means requires labeled data — it doesn't. K-Means is unsupervised: it groups points based purely on feature similarity, with no correct answers provided. A second trap is picking K purely by eyeballing without the elbow method or forgetting that different random centroid initializations can produce slightly different final clusterings.
✓ Quick Self-Check
1. Is K-Means a supervised or unsupervised algorithm?
Unsupervised — it groups data based on similarity with no labels provided.
Tap to reveal / hide
2. What does the elbow method plot to help choose K?
Inertia (cluster tightness) against different values of K, looking for the point where improvement sharply slows.
Tap to reveal / hide
3. What happens during the "move centroids" step?
Each centroid is recalculated as the mean position of all points currently assigned to that cluster.
Tap to reveal / hide
4. What is the main advantage of DBSCAN over K-Means?
DBSCAN finds arbitrarily shaped clusters and automatically detects outliers without requiring you to specify K in advance.
Tap to reveal / hide
5. How do you know K-Means has converged?
When the centroids stop moving significantly between rounds — the cluster assignments have stabilized.
Tap to reveal / hide