Grouping Data Points with K-Means Clustering

Last updated

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
Initial Clusters:
C1
C2

Clustering Solution

Iteration 1

Step 1: Calculate Euclidean Distances & Assign Clusters
Formula: d=(X2−X1)2+(Y2−Y1)2d = \sqrt{(X_2 - X_1)^2 + (Y_2 - Y_1)^2}
Distance Matrix
Starting:C1 [1, 5]C2 [8, 20]
Data PointCoordinatesDist to C1Dist to C2Assigned Cluster
P1[1, 5]016.553C1
P2[2, 6]1.41415.232C1
P3[1, 4]117.464C1
P4[8, 20]16.5530C2
P5[9, 22]18.7882.236C2
P6[8, 19]15.6521C2
P7[5, 12]8.0628.544C1
P8[9, 25]21.5415.099C2
Step 2: Calculate New Centroids (Means)
Formula: Cnew=(x1+x2+⋯+xnN,y1+y2+⋯+ynN)C_{new} = \left( \dfrac{x_1 + x_2 + \dots + x_n}{N}, \dfrac{y_1 + y_2 + \dots + y_n}{N} \right)
Cluster C14 points

Assigned Points:

P1, P2, P3, P7

Dim 1:(1 + 2 + 1 + 5) / 4= 2.25
Dim 2:(5 + 6 + 4 + 12) / 4= 6.75
Cluster C24 points

Assigned Points:

P4, P5, P6, P8

Dim 1:(8 + 9 + 8 + 9) / 4= 8.5
Dim 2:(20 + 22 + 19 + 25) / 4= 21.5

Iteration 2

Step 1: Calculate Euclidean Distances & Assign Clusters
Formula: d=(X2−X1)2+(Y2−Y1)2d = \sqrt{(X_2 - X_1)^2 + (Y_2 - Y_1)^2}
Distance Matrix
Starting:C1 [2.25, 6.75]C2 [8.5, 21.5]
Data PointCoordinatesDist to C1Dist to C2Assigned Cluster
P1[1, 5]2.15118.125C1
P2[2, 6]0.79116.808C1
P3[1, 4]3.02119.039C1
P4[8, 20]14.4441.581C2
P5[9, 22]16.6770.707C2
P6[8, 19]13.5322.55C2
P7[5, 12]5.92710.124C1
P8[9, 25]19.4583.536C2
Step 2: Calculate New Centroids (Means)
Formula: Cnew=(x1+x2+⋯+xnN,y1+y2+⋯+ynN)C_{new} = \left( \dfrac{x_1 + x_2 + \dots + x_n}{N}, \dfrac{y_1 + y_2 + \dots + y_n}{N} \right)
Cluster C14 points

Assigned Points:

P1, P2, P3, P7

Dim 1:(1 + 2 + 1 + 5) / 4= 2.25
Dim 2:(5 + 6 + 4 + 12) / 4= 6.75
Cluster C24 points

Assigned Points:

P4, P5, P6, P8

Dim 1:(8 + 9 + 8 + 9) / 4= 8.5
Dim 2:(20 + 22 + 19 + 25) / 4= 21.5

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.