Yes — there’s no built-in automatic cluster-count selection in vanilla k-means, but there are several well-established methods to estimate or automatically choose the optimal number of clusters k.
Below are the most common approaches, when they’re useful, and how “automatic” they can be made.
Plot inertia / SSE (sum of squared errors) vs. k.
Choose the k where the curve bends (“elbow”).
- Pros: Simple, fast
- Cons: The elbow is often ambiguous → not fully automatic
- Automation: You can algorithmically detect curvature changes (e.g., Kneedle algorithm)
Compute the mean silhouette score for each tested k.
- Score ranges from –1 to 1 (higher is better)
- Pick the k that maximizes the score
This is one of the simplest automatic approaches.
Compares the within-cluster dispersion to what would be expected under a reference null distribution.
- Very principled
- More computation-heavy
- Often gives clearer decisions than Elbow or Silhouette
Strictly speaking, this isn’t k-means — but many practitioners use:
Choose the model with the smallest BIC/AIC.
This is an automatic cluster count selection method and often outperforms k-means for non-spherical clusters.
X-means extends k-means by:
- Starting with a small k
- Locally trying to split clusters
- Using BIC to decide whether to split
➡️ Returns k automatically
This is widely considered the closest thing to “automatic k-means.”
Another extension of k-means that:
- Tests whether each cluster follows a Gaussian distribution
- If not, splits it
- Continues until all clusters pass the test
Also returns the optimal k automatically.
If you want a simple automatic method today:
If you want a true automatic algorithm:
✔️ Python code for any of these methods
✔️ Code to auto-select k and run k-means
✔️ A comparison of their runtime and reliability
Would you like a code example?