MACHINE LEARNING • LESSON 11

Choosing the Number of Clusters

K-Means needs us to choose K before it creates clusters. The challenge is deciding whether we should use 2, 3, 4, or another number of clusters.

THE SIMPLE IDEA

Don't choose K blindly.

We can try different values of K and compare how well the data is grouped. One common technique for doing this is called the Elbow Method.

01

Why Does K Matter?

Remember that K tells K-Means how many clusters to create.

K = 2 2 Groups

The algorithm creates two clusters.

K = 3 3 Groups

The algorithm creates three clusters.

K = 4 4 Groups

The algorithm creates four clusters.

Choosing a different K can produce completely different groups.

K is not just a setting. It directly controls how many groups K-Means creates.
02

What Happens If K Is Too Small?

Suppose the data naturally contains several different groups, but we choose:

K = 2

K-Means is forced to put everything into only two groups.

POSSIBLE PROBLEM Different groups may be combined together.

The clusters can become too broad and may not represent the structure of the data well.

03

What Happens If K Is Too Large?

Now imagine choosing a very large number of clusters.

K = 10

K-Means can create many small groups.

POSSIBLE PROBLEM The groups may become unnecessarily fragmented.

Instead of finding useful broad patterns, we may create too many tiny clusters.

The goal is not simply to use the largest possible K. We want a useful balance.
04

Try Different Values of K

Instead of guessing, we can test several values.

K 2
K 3
K 4
K 5
K 6

For each value, we measure how tightly the data points are grouped around their cluster centers.

K-Means provides a value called inertia, also known as within-cluster sum of squares (WCSS).

05

What Is Inertia?

Inertia measures how far data points are from the centroid of their cluster, using the sum of squared distances.

In simple terms:

LOW INERTIA Points are closer to their centers
HIGH INERTIA Points are farther from their centers

As we increase K, inertia normally decreases because more clusters give the algorithm more centers to work with.

Lower inertia is not enough by itself. If we keep increasing K, we can keep reducing inertia. We need to look for a useful point where the improvement starts becoming much smaller.
06

The Elbow Method

The Elbow Method is a common technique for choosing K.

We plot:

X-AXIS Number of Clusters (K)
Y-AXIS Inertia

A typical graph looks something like this:

Inertia
← Elbow
1    2    3    4    5    6
Number of Clusters (K)

The curve drops quickly at first and then starts to flatten. The point where the improvement noticeably slows down is called the elbow.

07

Simple Example

Suppose we test these values:

K INERTIA
1 1000
2 500
3 300
4 250
5 225

Notice the improvements:

K 1 → 2 Large improvement
K 2 → 3 Large improvement
K 3 → 4 Small improvement
K 4 → 5 Very small improvement
In this simplified example, K = 3 could be a reasonable choice because the improvement becomes much smaller after 3.

This does not mean K = 3 is automatically "correct." It means the Elbow Method gives us a useful reason to consider K = 3.

08

Python Example

We can test several values of K using Python.

from sklearn.cluster import KMeans

inertias = []

for k in range(1, 7):

    model = KMeans(
        n_clusters=k,
        random_state=42
    )

    model.fit(X)

    inertias.append(model.inertia_)

print(inertias)

Here we test:

K = 1 K = 2 K = 3 K = 4 K = 5 K = 6

The value:

model.inertia_

gives the inertia for that particular K.

We can then plot the K values against their inertia values and look for the elbow.

09

Don't Automatically Choose the Lowest Inertia

This is an important point.

Suppose we have:

K = 2 → 500 K = 3 → 300 K = 4 → 250 K = 5 → 225

K = 5 has lower inertia than K = 3.

But that doesn't automatically mean K = 5 is better.

Adding more clusters will generally make the points closer to their assigned centers, so inertia tends to keep decreasing.

The goal is not "minimum inertia at any cost." The goal is a useful number of clusters that captures meaningful structure without unnecessary complexity.
REMEMBER THIS

Choose K by looking for a useful balance.

Try several values of K, calculate the inertia for each, and use the Elbow Method to find where adding more clusters starts giving much smaller improvements.

1 Try K values
2 Measure inertia
3 Plot the curve
4 Find the elbow
QUICK CHECK

Check Your Understanding

What does K represent? The number of clusters K-Means creates.
Why shouldn't we choose K randomly? Different values of K can produce very different groupings.
What is inertia? A measure of the squared distances between points and their cluster centers.
What is the Elbow Method? A technique that uses the inertia curve to help choose a useful K.
Does inertia usually decrease when K increases? Yes. More clusters generally allow points to be closer to their centers.
Does the lowest inertia automatically mean the best K? No. We also want to avoid unnecessary clusters and look for the elbow.
NEXT TOPIC

Build K-Means With Python

Now that you understand what K-Means is, how it works, and how to choose K, we'll build a complete K-Means model using Python.