The gap statistic compares log⁡Wk (the log within-cluster dispersion) with its expectation under a reference null with no cluster structure. For k=1,…,5 suppose log⁡Wk=5.00, 4.20, 3.90, 3.75, 3.70, the reference expectations are 5.40, 4.75, 4.48, 4.20, 3.95, and all sk=0.05. Compute the gaps and choose k by the standard "one standard error" rule. Why not pick the k with the largest gap?

Unsupervised learning, Choosing k
Level L3 Intermediate. Length Medium. Nature Th+Mat Both.

What's being asked

Whether you can apply the gap statistic rule and understand why it prefers the smallest adequate k.

Key idea

Gap(k)=E∗[log⁡Wk]−log⁡Wk, where E∗ is the average over reference datasets (uniform over the data's range or box of its principal components). Choose the smallest k such that Gap(k)≥Gap(k+1)−sk+1, where sk+1 is the simulation standard error (scaled by 1+1/B for B reference sets).

Steps

  1. Gaps: 5.40−5.00=0.40, 4.75−4.20=0.55, 4.48−3.90=0.58, 4.20−3.75=0.45, 3.95−3.70=0.25.
  2. Test k=1: is 0.40≥0.55−0.05=0.50? No.
  3. Test k=2: is 0.55≥0.58−0.05=0.53? Yes. So the rule chooses k=2.
  4. The largest gap is at k=3 (0.58), but it is larger than the k=2 gap by only 0.03, which is less than the standard error 0.05. The difference is within simulation noise, so the simpler model wins.
  5. Explain the reason for the reference: log⁡Wk always decreases with k, so by itself it cannot select k. The gap measures how much faster it falls than it would for structureless data.
  6. Note that k=1 is allowed, so the method can say there are no clusters, which the elbow method cannot.

Common mistakes

How long your answer should be

Half a page; 4–5 minutes.

Last checked on 4 December 2026.

Read this

Previous in Unsupervised learning: CURE represents each cluster by several well-scattered representative points that are shrunk towards the cluster centroid by a factor α. In 2-D, a cluster with centroid (4,3) has a representative at (10,0). With α=0.2, where does it go? Why does the shrinking help, and what happens at α=0 and α=1?
Next in Unsupervised learning: Three points have pairwise distances d12=3, d23=4 and d13=7. Use classical multidimensional scaling (MDS) with double centring to find the matrix B and the 1-D embedding. Why is one dimension enough?