Skip to content
GATEiit

GATE DA · Machine Learning

Clustering algorithms: k-means and k-medoid

Partitioning n unlabelled points into k clusters: the k-means (Lloyd) algorithm and its SSE objective, initialisation and choice of k, and the outlier-robust k-medoids (PAM) method. GATE asks you to run iterations by hand and compute centroids, SSE and medoid costs.

Before you start

This topic comes before: Hierarchical clustering: top-down and bottom-up; single-linkage and multiple-linkage

Concept notes

1. Clustering and the k-means objective

Clustering groups unlabelled points so that points in the same cluster are similar and points in different clusters are not. It is unsupervised: there are no class labels.

k-means partitions n points into k clusters C1,…,Ck, each represented by its centroid μj (the mean of its points). It minimises the sum of squared errors (SSE, also called within-cluster sum of squares): SSE=∑j=1k∑x∈Cj∥x−μj∥2

For a fixed cluster, the point that minimises the sum of squared Euclidean distances is the mean. This is why k-means uses the mean as the centre. (The point minimising the sum of absolute distances in 1-D is the median.)

Finding the global minimum of SSE is NP-hard in general, so k-means is a heuristic that reaches a local minimum.

2. The k-means (Lloyd) algorithm

  1. Initialise: choose k initial centroids (for example k random data points).
  2. Assignment step: put each point in the cluster of its nearest centroid (Euclidean distance).
  3. Update step: recompute each centroid as the mean of the points assigned to it.
  4. Repeat steps 2–3 until the assignments (equivalently the centroids) stop changing.

Properties:

  • Each step can only lower SSE or leave it unchanged, so SSE is non-increasing, and since there are finitely many partitions the algorithm always terminates.
  • It converges to a local optimum that depends on the initial centroids.
  • Time per iteration is O(nkd) for n points in d dimensions; total O(nkdt) for t iterations. Fast in practice.
  • A centroid is usually not one of the data points.

If a cluster becomes empty, a common fix is to reinitialise its centroid (for example to the point farthest from its centre).

xy 135 135 C1, centroid (4/3, 4/3)C2, centroid (5, 5)
Final k-means clusters for the worked example: centroids (4/3, 4/3) and (5, 5) are marked with ×, and neither is a data point

3. Initialisation, choosing k, and limitations

Initialisation matters: a poor start can give a bad local minimum. Remedies:

  • run k-means several times from different random starts and keep the run with the lowest SSE;
  • k-means++: pick the first centre at random, then each next centre with probability proportional to D(x)2, the squared distance to the nearest centre already chosen. This spreads the centres out.

Choosing k: the minimum SSE never increases as k grows, and is 0 when k=n (each point its own cluster). So you cannot pick k by minimising SSE. The elbow method plots SSE against k and picks the k where the drop flattens. The silhouette score is another option.

Limitations of k-means:

  • k must be given in advance.
  • It favours compact, roughly spherical clusters of similar size; it fails on elongated or non-convex shapes (rings, crescents).
  • Sensitive to outliers: one far point drags the mean.
  • Needs a mean, so it works only on numeric data. (k-modes handles categorical data.)
  • Sensitive to attribute scale: normalize first.

4. k-medoids (PAM)

k-medoids represents each cluster by a medoid: an actual data point, the one with the least total distance to the others in its cluster. It minimises the total distance Cost=∑j=1k∑x∈Cjd(x,mj) for any distance d (often Manhattan). Because distances are not squared and the centre must be a real point, a single outlier has much less pull.

PAM (Partitioning Around Medoids):

  1. Choose k data points as initial medoids.
  2. Assign each point to its nearest medoid; compute the cost.
  3. For each medoid m and each non-medoid o, compute the cost if o replaces m (the swap). The change is Δ=Costnew−Costold.
  4. If the best swap has Δ<0, make it and go to step 2; otherwise stop.
k-means k-medoids (PAM)
Centre mean (need not be a data point) medoid (always a data point)
Objective sum of squared Euclidean distances sum of distances (any metric)
Outliers sensitive robust
Data numeric any data with a distance
Cost per iteration O(nkd) O(k(n−k)2)

PAM is slow on large data; CLARA runs PAM on samples, and CLARANS searches swaps at random.

Formulas to remember

k-means objective (SSE)
SSE=∑j=1k∑x∈Cj∥x−μj∥2
Centroid
μj=1∣Cj∣∑x∈Cjx
Euclidean distance
d(x,y)=∑i(xi−yi)2
Manhattan distance
d(x,y)=∑i∣xi−yi∣
k-medoids cost
Cost=∑j=1k∑x∈Cjd(x,mj)
k-means timet = number of iterations
O(n k d t)

Common mistakes

  • Updating a centroid as soon as one point is reassigned; standard (Lloyd) k-means reassigns all points first, then recomputes all centroids.
  • Forgetting to square the distances when computing SSE.
  • Choosing k by the lowest SSE; SSE keeps falling as k grows.
  • Taking a medoid as the mean or as any point; it must be the data point with the least total distance.
  • Using Euclidean distance in a k-medoids question that specifies Manhattan distance.
  • Stopping after one iteration without checking that the assignments no longer change.

Worked examples

Try each step yourself, then reveal the working.

Example 1: Running k-means by hand in 2-D

numerical

Points: A(1,1), B(2,1), C(1,2), D(5,4), E(6,5), F(5,6), G(4,5). Run k-means with k=2 and initial centroids A(1,1) and B(2,1). Give the final clusters, centroids and SSE.

  1. Step 1: Iteration 1: assign each point to the nearer of (1,1) and (2,1).

    Try it on paper first.

Example 2: One outlier: mean vs medoid

numerical

One cluster contains the 1-D points 1, 2, 3, 4, 20. Find its k-means centre and its medoid (with absolute distance).

  1. Step 1: Find the mean.

    Try it on paper first.

Example 3: A PAM swap

numerical

1-D points 1, 2, 3, 8, 9, 10, k=2, absolute distance. Initial medoids are 1 and 2. Evaluate the swap of medoid 1 with the non-medoid 9.

  1. Step 1: Compute the current cost.

    Try it on paper first.

Example 4: Is k-means the right tool?

conceptual

For each data set say whether plain k-means is suitable: (i) customer records with age (years) and income (rupees), three compact groups; (ii) two concentric rings of points; (iii) compact groups plus a few extreme outliers; (iv) records with only categorical attributes.

  1. Step 1: Check scale and shape for (i).

    Try it on paper first.

Previous year questions

2 official GATE DA questions on this topic, newest first.

Practise these 2 PYQs →

Topic test

10 questions: up to 2 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE DA syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.