How does the gap statistic determine the optimal number of clusters?

Last updated 11 November 2026

  • It compares log within-cluster dispersion W(k) on your data against its average on B structureless reference datasets (e.g. uniform over the bounding box): Gap(k) = mean_ref[log W(k)] − log W(k).

  • Choose the smallest k with Gap(k) ≥ Gap(k+1) − s(k+1), where s is the simulation standard error. This one-standard-error rule favors parsimony.

  • It can detect k = 1 (no structure), unlike silhouette. However, it is expensive, sensitive to the reference distribution, and assumes compact spherical clusters.

  • https://www.techmazone.com/pages/interview-k-means.html
  • https://www.geeksforgeeks.org/machine-learning/gap-statistics-for-optimal-number-of-cluster/
  • https://fahimfaisal.info/ml_and_llm_learning/23_clustering_evaluation/INTERVIEW_GRILL.html
All Clustering interview questions