MACHINE LEARNING • LESSON 11

How K-Means Works

K-Means works by repeatedly assigning data points to the nearest cluster center and then moving each center to the middle of its assigned points.

THE SIMPLE IDEA

Assign points → Move centers → Repeat

K-Means starts with some cluster centers, checks which center is closest to each data point, moves the centers, and repeats this process until the clusters become stable.

THE PROCESS

K-Means in 5 Simple Steps

01 Choose K

Decide how many clusters you want.

02 Choose Centers

Start with initial cluster centers.

03 Assign Points

Send each point to its nearest center.

04 Move Centers

Move each center to the middle of its group.

05 Repeat

Continue until the groups stop changing significantly.

STEP 1

Choose K

First, we decide how many clusters we want. This number is called K.

K = 2

This means we want K-Means to create 2 clusters.

K = 2 means two groups. K = 3 means three groups.
STEP 2

Choose Initial Centers

K-Means needs a starting point. It chooses K initial cluster centers.

These centers are called centroids.

C1 C2

C1 and C2 are the initial cluster centers.

At this stage, the centers may not be in their final positions. They are simply starting points.

STEP 3

Assign Each Point to the Nearest Center

Now K-Means looks at every data point and asks:

"Which centroid is closest to this point?"

The point is assigned to the closest centroid.

POINT A Closer to C1 → Cluster 1
POINT B Closer to C2 → Cluster 2

This happens for every data point.

Data Point Find Nearest Center Assign Cluster
STEP 4

Move the Centers

After all points have been assigned, K-Means looks at the points belonging to each cluster.

It calculates their average position and moves the centroid to that new position.

10 + 20 + 30 ÷ 3 = 20

Here, the mean is 20. K-Means uses this same idea to find the new center of a cluster.

The centroid moves toward the middle of the points that belong to its cluster.
STEP 5

Repeat the Process

Moving the centers can change which center is closest to some data points.

So K-Means repeats the process:

Assign Points to nearest center
Move Centers to the mean
Assign Again Check nearest center
Repeat Until stable
FULL EXAMPLE

Let's Follow K-Means With Customers

Suppose we have customers based on spending and number of purchases.

CUSTOMER SPENDING PURCHASES
A ₹1,000 2
B ₹1,200 3
C ₹10,000 15
D ₹11,000 17
K = 2

First Assignment

K-Means starts with two initial centroids. Each customer is assigned to whichever centroid is closer.

CLUSTER 1 A, B

These customers are closer to the first centroid.

CLUSTER 2 C, D

These customers are closer to the second centroid.

Move the Centers

Now K-Means calculates the average position of A and B and moves the first centroid toward their center.

It does the same for C and D.

A + B New Center 1 + C + D New Center 2

Check Again

K-Means checks the customers again using the new centers. If the assignments don't change, the algorithm can stop.

The goal is to find stable groups where each point belongs to the nearest cluster center.
WHY REPEAT?

Why Doesn't K-Means Do It Once?

Because the first centers are only starting points. They may not be in the best locations.

After points are assigned, the centers move. When the centers move, the nearest center can change.

Initial Centers
Assign Points
Move Centers
Better Centers
WHEN DOES IT STOP?

When the Clusters Become Stable

K-Means continues assigning points and moving centers until the algorithm reaches a stable solution, usually when the assignments or centers stop changing significantly.

BEFORE A B → Cluster 1 C D → Cluster 2
AFTER A B → Cluster 1 C D → Cluster 2

If the assignments stay the same, there is no useful reason to keep repeating the process.

IMPORTANT

K-Means Uses Distance

When K-Means decides which centroid is closest, it uses a distance calculation.

You don't need to memorize the mathematical formula yet. The important idea is:

Smaller distance = more similar / closer

For example, if a customer is closer to Center 1 than Center 2, that customer is assigned to Cluster 1.

REMEMBER THIS

K-Means keeps moving the centers until the groups become stable.

The complete process is: choose K, start with centers, assign points to the nearest center, calculate new centers, and repeat.

1 Choose K
2 Start Centers
3 Assign Points
4 Move Centers
5 Repeat
QUICK CHECK

Check Your Understanding

What is the first step? Choose the number of clusters, K.
What is a centroid? The center point representing a cluster.
How are points assigned? Each point is assigned to its nearest centroid.
Why do centroids move? They move to the average position of the points assigned to their cluster.
Why does K-Means repeat? Moving the centers can change which center is closest to some points.
When can it stop? When the cluster assignments or centers stop changing significantly.
NEXT TOPIC

Choosing the Number of Clusters

We now know how K-Means works. The next question is: how do we decide whether K should be 2, 3, 4, or another number?