Clustering groups unlabelled instances so that items in the same cluster are similar and items in different clusters are different. K-Means is the most widely used method.

The Algorithm

K-Means partitions the data into a chosen number k of clusters, each represented by its centroid (the mean of its points). It repeats two steps:

  1. Assignment: assign each point to the nearest centroid.
  2. Update: recompute each centroid as the mean of the points assigned to it.

The process starts from k initial centroids and stops when the assignments no longer change. It minimises the within-cluster sum of squares (inertia, SSE), the total squared distance of points to their own centroid.

Strengths and Weaknesses

K-Means is simple, fast and scales to large data. However, k must be chosen in advance, the result depends on the initial centroids (so the algorithm is run several times, or started with the smarter k-means++ method), it finds only round clusters of similar size, it is sensitive to outliers and it needs scaled numerical features.

Choosing k: the Elbow Method

Run K-Means for k = 1, 2, 3, ... and plot the inertia against k. Inertia always falls as k grows, but the fall becomes small once k reaches the natural number of clusters. The bend in the curve, the elbow, suggests a good k. The silhouette score, described later, is a second guide.

Definition: K-Means. An iterative clustering algorithm that divides data into k clusters by repeatedly assigning each point to the nearest centroid and recomputing each centroid as the mean of its cluster.

Table 4.2: K-Means steps and properties

Item Detail Note
Input Data and the number k k is chosen by the user
Step 1 Assign points to the nearest centroid Uses Euclidean distance
Step 2 Recompute centroids as means Stops when nothing changes
Objective Minimise inertia (SSE) Finds a local, not global, optimum

Example 4.2: K-Means on six numbers

Problem: Cluster the values 2, 3, 4, 10, 11, 12 into k = 2 clusters with initial centroids 2 and 12. Find the final centroids and the inertia, and the inertia for k = 1.

Solution:

  1. Assignment: 2, 3, 4 are nearer to 2; 10, 11, 12 are nearer to 12.
  2. Update: the new centroids are (2 + 3 + 4) / 3 = 3 and (10 + 11 + 12) / 3 = 11.
  3. Reassigning with centroids 3 and 11 gives the same clusters, so the algorithm stops.
  4. Inertia = (1 + 0 + 1) + (1 + 0 + 1) = 4.
  5. For k = 1 the centroid is 7 and the inertia is 25 + 16 + 9 + 9 + 16 + 25 = 100.

Result: Centroids 3 and 11, inertia 4 (against 100 for one cluster).

import numpy as np
from sklearn.cluster import KMeans
D = np.array([[2], [3], [4], [10], [11], [12]])
km = KMeans(n_clusters=2, init=np.array([[2], [12]]), n_init=1)
km.fit(D)
print("centroids:", km.cluster_centers_.ravel().tolist())
print("labels   :", km.labels_.tolist(), " inertia:", km.inertia_)
for k in (1, 2, 3):
    m = KMeans(n_clusters=k, n_init=10, random_state=0).fit(D)
    print("k =", k, "inertia =", round(m.inertia_, 2))

Output

centroids: [3.0, 11.0]
labels   : [0, 0, 0, 1, 1, 1]  inertia: 4.0
k = 1 inertia = 100.0
k = 2 inertia = 4.0
k = 3 inertia = 2.5

Common mistakes

  • Forgetting to scale features before clustering.
  • Assuming the result is the same on every run despite random initial centroids.
  • Choosing k by the lowest inertia, which is always at the largest k.
  • Applying K-Means to clusters of odd shapes or very different sizes.

Key takeaways

  • K-Means alternates assignment and centroid update.
  • It minimises the within-cluster sum of squares (inertia).
  • k must be chosen; use the elbow method or silhouette.
  • Use k-means++ and several runs to avoid poor starts.

Practice questions

  1. How is a centroid computed?
  2. What does the elbow method look at?
  3. Name one weakness of K-Means.

Answers to the practice questions

  1. As the mean of the points in the cluster.
  2. The inertia plotted against k; the bend suggests the best k.
  3. It needs k in advance, or it is sensitive to outliers and finds only round clusters.

Sources

  • Official BCA syllabus Introduction to Machine Learning, course code 26BCA404DSE21, Unit IV