K-means Clustering › Example

Michael BrenndoerferAugust 2, 20257 min read
Machine Learning from ScratchAll chapters
Part 7 of 9Unsupervised Learning
Chapter 53K-means Clustering › Example
Reading section 5 of 15 · Example · 7 minSection 5 of 15 · 7 min

Book contents

Section 5 of 15 · 7 min read

K-means Clustering › ExampleOne section · Sections in this chapter5 / 15 ↓

Let's work through a concrete example of K-means clustering using a simple 2D dataset with 8 data points. This step-by-step calculation will help us understand exactly how the algorithm works.

Given Data Points: We have 8 points in 2D space:

  • A(1, 1)A(1,\,1), B(1, 2)B(1,\,2), C(2, 1)C(2,\,1), D(2, 2)D(2,\,2)
  • E(8, 8)E(8,\,8), F(8, 9)F(8,\,9), G(9, 8)G(9,\,8), H(9, 9)H(9,\,9)

We want to find k=2k=2 clusters. Step 1: Initialization Let's randomly initialize two centroids:

  • Centroid 1: μ1=(1.5, 1.5)\mu_1 = (1.5,\ 1.5)
  • Centroid 2: μ2=(8.5, 8.5)\mu_2 = (8.5,\ 8.5)

Step 2: Assignment Step (First Iteration) For each point, we calculate the distance to both centroids and assign it to the nearest one.

For point A(1,1)A(1, 1):

  • Distance to μ1\mu_1: (1−1.5)2+(1−1.5)2=0.25+0.25=0.5≈0.707\sqrt{(1-1.5)^2 + (1-1.5)^2} = \sqrt{0.25 + 0.25} = \sqrt{0.5} \approx 0.707
  • Distance to μ2\mu_2: (1−8.5)2+(1−8.5)2=56.25+56.25=112.5≈10.61\sqrt{(1-8.5)^2 + (1-8.5)^2} = \sqrt{56.25 + 56.25} = \sqrt{112.5} \approx 10.61
  • AA is assigned to Cluster 1 (closer to μ1\mu_1)

For point E(8,8)E(8, 8):

  • Distance to μ1\mu_1: (8−1.5)2+(8−1.5)2=42.25+42.25=84.5≈9.19\sqrt{(8-1.5)^2 + (8-1.5)^2} = \sqrt{42.25 + 42.25} = \sqrt{84.5} \approx 9.19
  • Distance to μ2\mu_2: (8−8.5)2+(8−8.5)2=0.25+0.25=0.5≈0.707\sqrt{(8-8.5)^2 + (8-8.5)^2} = \sqrt{0.25 + 0.25} = \sqrt{0.5} \approx 0.707
  • EE is assigned to Cluster 2 (closer to μ2\mu_2)

Continuing this process for all points:

  • Cluster 1: A(1,1)A(1,1), B(1,2)B(1,2), C(2,1)C(2,1), D(2,2)D(2,2)
  • Cluster 2: E(8,8)E(8,8), F(8,9)F(8,9), G(9,8)G(9,8), H(9,9)H(9,9)

Step 3: Update Step (First Iteration)
Now we recalculate the centroids as the mean of points in each cluster.

For Cluster 1:
μ1=(1+1+2+24, 1+2+1+24)=(1.5, 1.5)\mu_1 = \left( \frac{1+1+2+2}{4},\ \frac{1+2+1+2}{4} \right) = (1.5,\ 1.5)

For Cluster 2:
μ2=(8+8+9+94, 8+9+8+94)=(8.5, 8.5)\mu_2 = \left( \frac{8+8+9+9}{4},\ \frac{8+9+8+9}{4} \right) = (8.5,\ 8.5)

Step 4: Convergence Check
The centroids haven't changed, so the algorithm has converged!

Final Result:

  • Cluster 1: {A,B,C,D}\{A, B, C, D\} with centroid (1.5, 1.5)(1.5,\ 1.5)
  • Cluster 2: {E,F,G,H}\{E, F, G, H\} with centroid (8.5, 8.5)(8.5,\ 8.5)

Verification:
Let's verify that no point would change clusters if we reassigned them:

  • All points in Cluster 1 are closer to (1.5, 1.5)(1.5,\ 1.5) than to (8.5, 8.5)(8.5,\ 8.5)
  • All points in Cluster 2 are closer to (8.5, 8.5)(8.5,\ 8.5) than to (1.5, 1.5)(1.5,\ 1.5)

The algorithm has successfully identified two distinct clusters in our data.

Let's visualize this step-by-step example to see how the algorithm works:

Out[12]:
Visualization
Scatter plot showing initial data points with true cluster structure.
Step 1: Initial data points with true cluster structure. The 8 data points are clearly separated into two natural clusters: points A, B, C, D form one cluster in the lower-left region, while points E, F, G, H form another cluster in the upper-right region. This visualization shows the ground truth that K-means should ideally discover.
Scatter plot showing initial centroid placement and cluster assignments.
Step 2: Random initialization and first assignment. Two initial centroids are placed at (1.5, 1.5) and (8.5, 8.5), and each data point is assigned to its nearest centroid. The dashed lines show the assignment relationships.
Scatter plot showing final converged result with Voronoi boundaries.
Step 3: Final converged result with cluster boundaries. The algorithm has converged with centroids at the same positions as the initial centroids, indicating that the initial placement was optimal. The black contour lines show the Voronoi diagram, which represents the decision boundaries that separate the two clusters in the feature space.

Here you can see the complete K-means process on our simple 8-point example, showing how the algorithm successfully identifies the two natural clusters in the data.

In this example we've chosen random initial centroids that conveniently were located in the middle of the two clusters to make the example simple. In practice, we should use the k-means++ initialization method to choose the initial centroids.