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.
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.
K-Means in 5 Simple Steps
Decide how many clusters you want.
Start with initial cluster centers.
Send each point to its nearest center.
Move each center to the middle of its group.
Continue until the groups stop changing significantly.
Choose K
First, we decide how many clusters we want. This number is called K.
This means we want K-Means to create 2 clusters.
Choose Initial Centers
K-Means needs a starting point. It chooses K initial cluster centers.
These centers are called centroids.
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.
Assign Each Point to the Nearest Center
Now K-Means looks at every data point and asks:
The point is assigned to the closest centroid.
This happens for every data point.
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.
Here, the mean is 20. K-Means uses this same idea to find the new center of a cluster.
Repeat the Process
Moving the centers can change which center is closest to some data points.
So K-Means repeats the process:
Let's Follow K-Means With Customers
Suppose we have customers based on spending and number of purchases.
First Assignment
K-Means starts with two initial centroids. Each customer is assigned to whichever centroid is closer.
These customers are closer to the first centroid.
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.
Check Again
K-Means checks the customers again using the new centers. If the assignments don't change, the algorithm can stop.
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.
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.
If the assignments stay the same, there is no useful reason to keep repeating the process.
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:
For example, if a customer is closer to Center 1 than Center 2, that customer is assigned to Cluster 1.
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.