AI Computer Institute
Expert-curated CS & AI curriculum aligned to CBSE standards. A bharath.ai initiative. About Us

K-Means Clustering: Grouping Similar Data

📚 Machine Learning⏱️ 18 min read🎓 Grade 9
✍️ AI Computer Institute Editorial Team Updated: August 2026 CBSE-aligned · Peer-reviewed · 18 min read
Content curated by subject matter experts with IIT/NIT backgrounds. All chapters are fact-checked against official CBSE/NCERT syllabi.

The Problem: Sorting Players With No Labels to Go By

Imagine you are helping an IPL franchise's analytics team shortlist 6 uncapped batters before the mini-auction. For each player, the team has boiled down a season of scorecards into two simple 0-60 numbers: a Consistency Index (built from batting average and how rarely they get out cheaply) and an Attack Index (built from strike rate and boundary percentage). Nobody has told you in advance which players are "anchors" and which are "finishers" — there are no labels in the spreadsheet at all, just two numbers per player. Your job is to look at the numbers and split the six players into two natural groups so the team can plan its bidding strategy for each type.

This is exactly the kind of problem K-Means Clustering was built to solve: given a pile of unlabeled data, find groups of points that are close to each other, without anyone telling the algorithm what the groups mean or even what a "correct" grouping looks like.

Supervised vs. Unsupervised: Why This Problem Is Different

In earlier machine learning topics you may have seen algorithms that learn from labeled examples — you show the computer thousands of emails already marked "spam" or "not spam," and it learns a rule to label new emails. That is called supervised learning, because a supervisor (the labels) tells the algorithm the right answer during training.

K-Means belongs to a different family called unsupervised learning. There are no labels anywhere in the data. Nobody has marked any player "anchor" or "finisher" in advance. The algorithm's only job is to notice that some points sit close together in a corner of the graph and other points sit close together in a different corner, and to draw a boundary between those two clumps. It discovers structure that was already hiding in the numbers — it does not predict a label that a human already assigned.

Starting Simple: Grouping Numbers on a Line

Before dealing with two numbers per player, strip the problem down to one number, so the core idea is easy to see. Suppose six students scored these marks (out of 20) on a quiz: 4, 5, 6, 15, 16, 18. If someone asked you to split these six marks into two groups, you would almost certainly say "4, 5, 6 in one group, and 15, 16, 18 in the other," without doing any formal calculation. Your eye is doing something specific: it is looking for a gap. The numbers 4, 5, and 6 are close to each other (at most 2 apart), and 15, 16, and 18 are close to each other (at most 3 apart), but the two clumps are separated by a large gap of 9 (from 6 to 15).

That instinct — "things that are numerically close belong together" — is the entire soul of K-Means. The algorithm just makes that instinct precise and repeatable, and extends it from one number to as many numbers (dimensions) as your data has.

From a Line to a Plane: Measuring "Closeness" With Two Numbers

Once each player has two scores instead of one, "closeness" is no longer just subtraction. You need to measure distance between two points on a plane, and for that K-Means uses the same formula you already know from geometry class: the Pythagorean theorem.

If point P1 is at (x1, y1) and point P2 is at (x2, y2), draw a right triangle between them: one leg has length (x1 − x2), the other leg has length (y1 − y2), and the straight-line distance between the two points is the hypotenuse. By Pythagoras,

distance = √[ (x1 - x2)² + (y1 - y2)² ]

This is called Euclidean distance, and it is simply "how far apart are these two points on the graph," measured the way you'd measure it with a ruler. K-Means uses this formula (or a small shortcut on it, explained below) to decide which points are "close" to which.

A useful shortcut: if all you want to know is which of two centroids a point is closer to, you don't actually need the square root. Since square-rooting never changes which of two non-negative numbers is bigger, comparing the squared distances, (x1−x2)² + (y1−y2)², gives the exact same "closer/farther" answer as comparing the true distances — and it's faster to compute by hand or by machine. K-Means implementations use this trick constantly.

The K-Means Algorithm, Step by Step

"K" in K-Means simply stands for the number of groups you want — you choose it before the algorithm starts. If you want 2 groups, K = 2; if you want 5, K = 5. The algorithm then repeats a simple two-step loop:

  1. Choose K, the number of clusters you want, and pick K starting points to act as centroids (the "center" of each cluster). A common, simple choice is to just grab K of the actual data points at random to start from.
  2. Assignment step: for every data point, measure its distance to each of the K centroids, and assign that point to whichever centroid is nearest.
  3. Update step: for each cluster, recompute its centroid as the mean (average x, average y) of every point now assigned to it. The centroid physically moves to sit at the middle of its new group.
  4. Repeat steps 2 and 3. Each time, some points may switch which centroid they're closest to, and the centroids shift again. Stop when a full round changes nothing — every point stays with the same centroid it had before, and the centroids stop moving. This stable state is called convergence.

Notice what makes this algorithm elegant: it alternates between two very different jobs — "which group is each point closest to?" and "given these groups, where's the true middle?" — and each job makes the other job's answer slightly more accurate, until neither job has anything left to fix.

Worked Example: Clustering Six Players by Hand

Here are the six players' index scores as (Consistency, Attack) pairs:

  • A = (10, 20)
  • B = (12, 24)
  • E = (11, 22)
  • C = (50, 40)
  • D = (48, 36)
  • F = (52, 44)

We want K = 2 clusters. Let's initialize by picking two actual data points as our starting centroids: C1 = A = (10, 20) and C2 = D = (48, 36).

Iteration 1 — Assignment step. For each point, compute the squared distance to C1 and to C2, and keep whichever is smaller.

Point A(10,20):  to C1: (0)²+(0)²   = 0     to C2: (38)²+(16)² = 1700   -> nearer C1
Point B(12,24):  to C1: (2)²+(4)²   = 20    to C2: (36)²+(12)² = 1440   -> nearer C1
Point E(11,22):  to C1: (1)²+(2)²   = 5     to C2: (37)²+(14)² = 1565   -> nearer C1
Point C(50,40):  to C1: (40)²+(20)² = 2000  to C2: (2)²+(4)²   = 20     -> nearer C2
Point D(48,36):  to C1: (38)²+(16)² = 1700  to C2: (0)²+(0)²   = 0      -> nearer C2
Point F(52,44):  to C1: (42)²+(24)² = 2340  to C2: (4)²+(8)²   = 80     -> nearer C2

So Cluster 1 = {A, B, E} and Cluster 2 = {C, D, F}.

Iteration 1 — Update step. Move each centroid to the average of its cluster's points.

New C1 = ( (10+12+11)/3 , (20+24+22)/3 ) = (33/3, 66/3)  = (11, 22)
New C2 = ( (50+48+52)/3 , (40+36+44)/3 ) = (150/3, 120/3) = (50, 40)

The centroids moved: C1 went from (10, 20) to (11, 22), and C2 went from (48, 36) to (50, 40) — each centroid slid toward the true middle of the points that picked it.

Iteration 2 — Assignment step, again. Recheck every point against the new centroids (11, 22) and (50, 40). Because the new centroids are still much closer to their own three points than to the other three, every point is assigned to the same cluster as before — {A, B, E} still choose C1, and {C, D, F} still choose C2. Nothing changed.

Convergence. Since no point switched clusters, recomputing the averages gives back the exact same centroids, (11, 22) and (50, 40). The algorithm has converged after just one real round of movement: Cluster 1 is the "anchor" group centered near (11, 22) — low consistency and low attack scores relative to the other group — and Cluster 2 is the "aggressive" group centered near (50, 40).

Seeing the Clusters and Their Centroids

The diagram below plots all six players on the Consistency-Attack plane. The two centroids sit at the center of mass of their group, and the dashed lines show which centroid each point was assigned to.

K-Means Result: K = 2 Clusters 0 20 40 60 Consistency Index 0 25 50 Attack Index A B E C D F Cluster 1 (anchors) Cluster 2 (aggressive) Centroid

Coding It: A Six-Line K-Means in Python

The hand calculation above is exactly what the following code does — it just does it faster and can handle far more than six points. Trace through it and check it against the arithmetic you just did.

points = [(10,20), (12,24), (50,40), (48,36), (11,22), (52,44)]
centroids = [(10,20), (48,36)]   # start: use points A and D as centroids

def sq_dist(p, c):
    return (p[0] - c[0]) ** 2 + (p[1] - c[1]) ** 2

for iteration in range(2):
    clusters = {0: [], 1: []}
    for p in points:
        distances = [sq_dist(p, c) for c in centroids]
        nearest = distances.index(min(distances))
        clusters[nearest].append(p)

    new_centroids = []
    for k in clusters:
        xs = [p[0] for p in clusters[k]]
        ys = [p[1] for p in clusters[k]]
        new_centroids.append((sum(xs) / len(xs), sum(ys) / len(ys)))
    centroids = new_centroids

    print(f"After iteration {iteration + 1}: centroids = {centroids}")

Running this prints:

After iteration 1: centroids = [(11.0, 22.0), (50.0, 40.0)]
After iteration 2: centroids = [(11.0, 22.0), (50.0, 40.0)]

The centroids after iteration 1 match the hand-calculated (11, 22) and (50, 40) exactly. Iteration 2 prints the identical numbers — proof, straight from the program's own output, that the clusters had already converged and nothing moved on the second pass. In a real implementation, you would not hardcode range(2); you would loop while centroids keep changing, but here we know from the hand-worked example that two passes are enough.

Choosing K: The Elbow Method

In the player example, we picked K = 2 because we already suspected two natural roles. But what if you don't know how many groups make sense — should the auction committee use 2 categories, 3, or 5? K-Means will not tell you; it will happily produce whatever K you ask for, even a bad one. Ask for K = 6 on our six players, and it will just place one centroid on top of each player — technically valid, but useless.

A common way to pick a reasonable K is the elbow method. For each candidate value of K (1, 2, 3, 4, ...), run K-Means and measure the within-cluster sum of squares (WCSS): add up the squared distance from every point to its own centroid. WCSS always drops as K increases (more centroids can only reduce distances, never increase them), so you cannot just pick the K with the lowest WCSS — that would always be "one centroid per point." Instead, plot WCSS against K. Early on, adding centroids helps a lot and WCSS drops sharply; after some point, adding more centroids barely helps because you're just splitting already-tight clusters further. The graph bends like an elbow at that point, and the K at the bend is usually the most sensible choice — enough groups to capture real structure, not so many that you're just carving up noise.

Two Misconceptions to Fix Now

Misconception 1: "K-Means figures out how many groups exist on its own." It does not. K is a number you must choose and hand to the algorithm before it runs. K-Means only answers "given exactly K groups, where should the boundaries go?" It has no built-in sense of the "right" number of clusters; that judgment call is yours, and tools like the elbow method only help you make it — they don't make it automatically.

Misconception 2: "Since it groups similar things, K-Means will find any natural shape of grouping." Also false. Because K-Means always assigns a point to whichever centroid is nearest by straight-line distance, every cluster it produces ends up roughly round or blob-shaped, radiating out from its centroid. If your real data forms an elongated shape, or two curved crescents wrapped around each other, or one small tight cluster next to one huge spread-out cluster, K-Means will cut straight through those shapes in ways that don't match how a human would group them. K-Means clusters are always, by construction, closer to circular blobs of similar size — it is not a general-purpose "find any pattern" tool.

One more practical wrinkle worth knowing: because the starting centroids are often chosen randomly, running K-Means twice on the same data with different random starting points can occasionally produce different final clusters, especially if the data doesn't have one obviously best grouping. This is why real implementations often run K-Means several times with different random starts and keep the result with the lowest WCSS, rather than trusting a single run.

Where K-Means Shows Up in Practice

Beyond sorting cricket players, the identical algorithm is used to group customers by purchase behavior so a company can design different offers for different segments, to compress images by replacing millions of exact pixel colors with the nearest of just K representative colors, and to group news articles or documents by topic when nobody has manually tagged them. In every case, the underlying question is the same one you just solved by hand: given a pile of points with no labels, which ones are close enough to each other to call a group, and where does each group's center sit?

Practice: Test Yourself

  1. Three points are P1 = (0, 0), P2 = (3, 4), and P3 = (6, 8). Using the Pythagorean-based distance formula, find the actual (not squared) Euclidean distance from P1 to P2, and from P1 to P3. What do you notice about the relationship between the two answers?
  2. A dataset has centroids C1 = (2, 2) and C2 = (10, 2). A new point P = (5, 2) needs to be assigned. Compute the squared distance from P to each centroid and state which cluster P joins. Then explain in one sentence why using squared distance instead of true distance still gives the correct answer here.
  3. You run K-Means with K = 3 on a dataset and, after convergence, one of the three clusters contains only a single point sitting far from everything else. Is this a bug in the algorithm? Explain what actually happened.
  4. Explain, in your own words, why K-Means is called "unsupervised" learning while a spam-detection model trained on emails marked "spam"/"not spam" is called "supervised" learning.
  5. A friend says, "I ran K-Means with K = 4, so the data must have exactly 4 natural groups." Identify the misconception in this statement and correct it.

Summary

K-Means Clustering is an unsupervised algorithm that groups unlabeled data points into K clusters purely by closeness. It works by repeating two steps: assign every point to its nearest centroid (using Euclidean, Pythagoras-based distance, or the faster squared-distance shortcut), then move each centroid to the average position of the points now assigned to it. This loop repeats until nothing changes — convergence. You must choose K yourself before running the algorithm; the elbow method, which tracks how within-cluster sum of squares drops as K grows, is a common way to pick a sensible value. K-Means clusters are always roughly round because they're built from distance-to-centroid, so shapes like crescents, rings, or very unevenly-sized groups can defeat it, and different random starting centroids can occasionally produce different final answers on the same data. The same six-line loop you traced by hand and in code here — assign, average, repeat — is the exact algorithm used industrially for customer segmentation, image color compression, and document grouping.

← ROC Curves and AUC: Understanding Model PerformanceRandom Forests: Ensemble Learning Power →

Found this useful? Share it!

📱 WhatsApp 🐦 Twitter 💼 LinkedIn