# How Many Groups?

With no answer key, the number of groups k is chosen with evidence and judgment, inertia (the within-cluster sum of squares) keeps falling as k grows so you look for the bend where it stops falling fast, the silhouette score from −1 to 1 says how well each example sits in its group, and in the end what the groups are for decides.

Freelancer · AI Basics · Python · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Compare KMeans inertia across several values of k and find the elbow, and explain why the lowest inertia is never the answer
- Score groupings with silhouette_score from −1 to 1, and explain the limits of the elbow and the silhouette and why the groups' use decides k

## Key takeaways

- Inertia, the total squared distance from each example to its center, keeps falling as k grows, so the lowest inertia always says "more groups" and is never the answer.
- The elbow is the k where inertia stops falling fast. It's a rule of thumb, and the bend can be hard to see.
- The silhouette score runs from −1 to 1. Near 1 means examples sit well inside their groups, near 0 means groups overlap, below 0 means examples are likely in the wrong group.
- Both measures favor round, compact groups, and the silhouette needs at least 2 groups.
- There's no true number of groups. The evidence narrows it down, and what the groups are for decides.

The café's owner asks the obvious question: how many kinds of regulars do we have? k-means can't tell her. You give it k, and it finds k groups, whether or not the data has k groups in it. With no answer key, choosing k is a judgment backed by evidence, and every clustering you do needs one. This lesson gives you two measures, what each can't tell you, and the question that settles it.

The example is the café's 600 members in `cafe_members.csv`, which comes with *Exercise: Café Regulars*. Each code block below is one notebook cell, and the cells run in order.

## Why the tightest isn't the answer

*k-means with scikit-learn* used `inertia_`, the total squared distance from each member to its group's center. scikit-learn's guide calls it the **inertia**, or, in your textbook, the *within-cluster sum of squares*: smaller means tighter groups. Here it is for k from 1 to 8, on the scaled features:

```python
import pandas as pd
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
from sklearn.preprocessing import StandardScaler

members = pd.read_csv("cafe_members.csv")
features = ["visits_per_month", "avg_spend", "share_before_9am", "share_with_food"]
X_scaled = StandardScaler().fit_transform(members[features])

for k in range(1, 9):
    kmeans = KMeans(n_clusters=k, n_init=10, random_state=0).fit(X_scaled)
    print("k =", k, "inertia:", round(kmeans.inertia_, 1))
```

```text
k = 1 inertia: 2400.0
k = 2 inertia: 1166.3
k = 3 inertia: 502.2
k = 4 inertia: 298.8
k = 5 inertia: 265.0
k = 6 inertia: 237.1
k = 7 inertia: 214.0
k = 8 inertia: 199.3
```

With one group, every member is measured from the overall middle: 2,400, which is 600 members times 4 scaled features, each with a spread of 1. Every extra center gives some members a nearer one, so inertia keeps falling. Give every member its own group and it reaches 0, a perfect score for a useless answer. So the lowest inertia always says "more groups". It can't choose k on its own, and scikit-learn's guide adds that it isn't a normalized measure: lower is better, but 298.8 means nothing alone.

## The elbow

Look at how much each extra group buys: from k = 1 to 2, inertia falls by 1,234; then by 664, then 203. From 4 to 5 it falls only 34, then 28. The big drops stop after 4. Plotted, the line falls steeply and then flattens, with a bend like an elbow at k = 4. Choosing k at that bend is the **elbow method**, a rule of thumb: past it, each new group splits a real group instead of finding a new one.

![Two charts side by side from the café's 600 scaled members: on the left, inertia up the side against k from 1 to 8 across the bottom, a line falling steeply from 2400.0 at k = 1 through 1166.3 and 502.2 to 298.8 at k = 4, then flattening through 265.0, 237.1 and 214.0 to 199.3 at k = 8, with the bend at k = 4 marked "elbow"; on the right, the silhouette score up the side against k from 2 to 8, rising from 0.492 at k = 2 to 0.577 at k = 3 and a peak of 0.586 at k = 4, then falling through 0.537, 0.424 and 0.367 to 0.282 at k = 8](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/de74d7b18ec64238b5d41b6a1ff9a91e.webp)

Here the bend is clear because the data was planted with four round groups. On real data the line often curves smoothly, and two people can see the elbow at different places.

## The silhouette score

The **silhouette score** asks a sharper question of each member: is it closer to its own group than to the next nearest group? For one member, *a* is its mean distance to the other members of its group, and *b* its mean distance to the members of the nearest other group. Its silhouette is (b − a) ÷ the larger of a and b, read "b minus a, over the larger of the two". A member with a = 2 and b = 8 scores (8 − 2) ÷ 8 = 0.75: well inside its group. Near 1 means well inside, near 0 means on the border between two groups, and below 0 means it's probably in the wrong group. `silhouette_score` gives the mean over all members:

```python
for k in range(2, 9):
    labels = KMeans(n_clusters=k, n_init=10, random_state=0).fit_predict(X_scaled)
    print("k =", k, "silhouette:", round(silhouette_score(X_scaled, labels), 3))
```

```text
k = 2 silhouette: 0.492
k = 3 silhouette: 0.577
k = 4 silhouette: 0.586
k = 5 silhouette: 0.537
k = 6 silhouette: 0.424
k = 7 silhouette: 0.367
k = 8 silhouette: 0.282
```

Unlike inertia, it doesn't keep improving: it peaks at k = 4. But k = 3 is close behind, so the silhouette points at 4 without ruling 3 out. It needs two groups at least, since a member with no other group has no *b*:

```python
labels = KMeans(n_clusters=1, n_init=10, random_state=0).fit_predict(X_scaled)
silhouette_score(X_scaled, labels)
```

```text
ValueError: Number of labels is 1. Valid values are 2 to n_samples - 1 (inclusive)
```

Both measures share a limit: they reward round, compact groups, as k-means looks for. scikit-learn's guide says the silhouette is generally higher for convex clusters than for other shapes of group, so a high score means "k-means-shaped groups", not "true groups".

## What the groups are for

The evidence narrows it to 3 or 4. To choose, look at what each would hand the owner:

```python
three = KMeans(n_clusters=3, n_init=10, random_state=0).fit_predict(X_scaled)
print(members[features].groupby(three).mean().round(2))
print(pd.Series(three).value_counts().sort_index().tolist())
```

```text
   visits_per_month  avg_spend  share_before_9am  share_with_food
0              6.33       8.49              0.28             0.38
1              4.16      21.19              0.10             0.84
2             17.46       5.50              0.85             0.15
[298, 122, 180]
```

With 3, the occasional visitors (about 3 visits a month) and the afternoon regulars (about 10) merge into one group of 298 members, averaging 6.33 visits: a group that describes almost nobody in it. If the owner wants a plan to bring back people who rarely come, she needs them apart, so k = 4. If she only plans a breakfast deal and a brunch menu, 3 might do. scikit-learn's guide is plain about it: there's no single true number of clusters, and the number is decided from data-based criteria and knowledge of what the groups are for. Choose with the elbow, the silhouette and the use together, and say which you used.

## Check yourself

2 questions to try on the lesson page.

## So what

No measure picks k for you: inertia always wants more groups, the elbow and the silhouette narrow the choice, and what the groups are for makes the call.

## Sources

- scikit-learn 1.9.1, [Clustering: K-means](https://scikit-learn.org/stable/modules/clustering.html#k-means), User Guide: k-means chooses centroids that minimize the inertia, or within-cluster sum-of-squares; inertia assumes convex, isotropic clusters, and is not a normalized metric (lower values are better and zero is optimal).
- scikit-learn 1.9.1, [Clustering performance evaluation: Silhouette Coefficient](https://scikit-learn.org/stable/modules/clustering.html#silhouette-coefficient), User Guide: for each sample, *a* is the mean distance to the other points in its cluster and *b* the mean distance to the points in the next nearest cluster, s = (b − a) / max(a, b), and the score for a set is the mean; it's bounded between −1 for incorrect clustering and +1 for highly dense clustering, scores around zero indicate overlapping clusters, and it's generally higher for convex clusters than for other concepts of clusters.
- scikit-learn 1.9.1, [silhouette_score](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.silhouette_score.html), API reference: the best value is 1 and the worst −1, values near 0 indicate overlapping clusters, negative values generally mean a sample was assigned to the wrong cluster, and it's only defined for 2 ≤ number of labels ≤ n_samples − 1.
- scikit-learn 1.9.1, [Selecting the number of clusters with silhouette analysis on KMeans clustering](https://scikit-learn.org/stable/auto_examples/cluster/plot_kmeans_silhouette_analysis.html), example: silhouette analysis is used to assess the number of clusters, and it can be ambivalent between two values.
- scikit-learn 1.9.1, [Demonstration of k-means assumptions](https://scikit-learn.org/stable/auto_examples/cluster/plot_kmeans_assumptions.html), example: there is no uniquely defined true number of clusters; an appropriate number has to be decided from data-based criteria and knowledge of the intended goal.
- scikit-learn 1.9.1, [KMeans](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html), API reference: `inertia_` is the sum of squared distances of samples to their closest cluster center.

## Key terms

- [Inertia](https://www.ask-codey.com/glossary/inertia)
- [Elbow Method](https://www.ask-codey.com/glossary/elbow-method)
- [Silhouette Score](https://www.ask-codey.com/glossary/silhouette-score)
- [K-Means Clustering](https://www.ask-codey.com/glossary/k-means-clustering)

## Related

- [k-means with scikit-learn](https://www.ask-codey.com/lessons/ml-k-means-with-scikit-learn)
- [Describing Each Group](https://www.ask-codey.com/lessons/ml-describing-clusters)
- [Exercise: Café Regulars](https://www.ask-codey.com/lessons/exercise-ml-cafe-regulars)

Source: https://www.ask-codey.com/lessons/ml-choosing-k
