Grouping Data Points with K-Means Clustering
Problem Setup
Scenario:Group users into two engagement clusters based on their daily sessions and average session duration.
Given:Eight data points are described by two numerical features: Daily_Sessions and Avg_Session_Duration_mins. The number of clusters is set to 2, with P1 and P4 used as the initial centroids.
Task:Use the K-Means algorithm to assign each data point to the nearest centroid using Euclidean distance, recompute the cluster centroids from the assigned points, and repeat the process until the cluster assignments stabilize.
Need a quick refresher? Review K-Means theory
Data Points & Initial Centroids
Each row represents a user using two features: daily sessions and average session duration in minutes. P1 and P4 are selected as the starting centroids for the two clusters.
| Data Point | Daily_Sessions✎ | Avg_Session_Duration_mins✎ |
|---|---|---|
| P1 | 1 | 5 |
| P2 | 2 | 6 |
| P3 | 1 | 4 |
| P4 | 8 | 20 |
| P5 | 9 | 22 |
| P6 | 8 | 19 |
| P7 | 5 | 12 |
| P8 | 9 | 25 |
Clustering Solution
Iteration 1
Step 1: Calculate Euclidean Distances & Assign Clusters
| Data Point | Coordinates | Dist to C1 | Dist to C2 | Assigned Cluster |
|---|---|---|---|---|
| P1 | [1, 5] | 0 | 16.553 | C1 |
| P2 | [2, 6] | 1.414 | 15.232 | C1 |
| P3 | [1, 4] | 1 | 17.464 | C1 |
| P4 | [8, 20] | 16.553 | 0 | C2 |
| P5 | [9, 22] | 18.788 | 2.236 | C2 |
| P6 | [8, 19] | 15.652 | 1 | C2 |
| P7 | [5, 12] | 8.062 | 8.544 | C1 |
| P8 | [9, 25] | 21.541 | 5.099 | C2 |
Step 2: Calculate New Centroids (Means)
Assigned Points:
P1, P2, P3, P7
Assigned Points:
P4, P5, P6, P8
Iteration 2
Step 1: Calculate Euclidean Distances & Assign Clusters
| Data Point | Coordinates | Dist to C1 | Dist to C2 | Assigned Cluster |
|---|---|---|---|---|
| P1 | [1, 5] | 2.151 | 18.125 | C1 |
| P2 | [2, 6] | 0.791 | 16.808 | C1 |
| P3 | [1, 4] | 3.021 | 19.039 | C1 |
| P4 | [8, 20] | 14.444 | 1.581 | C2 |
| P5 | [9, 22] | 16.677 | 0.707 | C2 |
| P6 | [8, 19] | 13.532 | 2.55 | C2 |
| P7 | [5, 12] | 5.927 | 10.124 | C1 |
| P8 | [9, 25] | 19.458 | 3.536 | C2 |
Step 2: Calculate New Centroids (Means)
Assigned Points:
P1, P2, P3, P7
Assigned Points:
P4, P5, P6, P8
Final Answer
Converged after:2 iterations
Final clusters:C1: P1, P2, P3, P7 → [2.25, 6.75]; C2: P4, P5, P6, P8 → [8.5, 21.5].
The second iteration produces the same cluster assignments as the first. Recalculating the means therefore leaves both centroids unchanged, confirming that K-Means has converged.
Exam Takeaway
- Compare every point with every current centroid: Calculate the distance to each centroid before assigning a point. When distances are equal, follow the specified tie-breaking rule rather than choosing arbitrarily.
- Recalculate centroids from the newly assigned points: Use the coordinate-wise mean of each cluster, not the original centroid values. These updated means must be used in the next iteration.
- Verify convergence using consecutive iterations: Compare the new assignments with the previous ones. If no point changes clusters and the centroids remain unchanged, the algorithm has converged. Do not stop merely because the clusters appear well separated.