AI foundations · 08

Can a computer find groups nobody labeled?

Yes. A clustering method sorts examples into groups by how close their measurements are, with no answers given in advance. But the computer only finds groups of numbers. What a group means, and whether it matters, is for people to check, and different measurements or a different number of groups give different groups.

Grades 6+15 minutesLight math: averages and squaresSimpler version for grades 3–5

What you will be able to do

Run the k-means method by hand on a few points, explain why its result depends on the number of groups, the starting centers and the chosen measurements, and explain why a person has to check what a discovered group means.

Before you start

Read How can examples become a rule? first, for the words feature and label. You need to know how to average a few numbers.

1 · You have already seen this

Groups that nobody named in advance

Some apps sort your things into groups without being told what the groups are.

A photo app can put similar-looking pictures together so you can sort them faster, though nobody told it what is in them. A music service can group songs with similar patterns, and then a person decides what each group is for, such as a playlist theme. If you have ever sorted a pile of picture cards with no categories given, you know there is more than one way to do it, by color or by size.

This is unsupervised learning: the computer gets measurements but no answers, and looks for structure in them. The most common job is clustering, which puts similar examples into groups. This page uses made-up songs measured by tempo and energy. It shows how that works, lets you try it, and shows where it goes wrong.

2 · What happens, step by step

From measurements to groups

Clustering with k-means in five steps. The next section goes deeper into step 1, the one after it into steps 2 to 4, and the lab lets you run steps 3 and 4 yourself.

How k-means clustering works

Five steps

  1. 1 · Measure every exampleEach example gets the same measurements, its features, such as tempo and energy for each song. Nobody supplies labels.
  2. 2 · Choose the number of groupsYou pick k, the number of groups you want. The computer places k centers as starting points.
  3. 3 · AssignEvery example joins the center nearest to it.
  4. 4 · Move and repeatEvery center moves to the average position of the examples that joined it. Assign and move repeat until no example changes group.
  5. 5 · Look at the groupsThe computer only gives you “group 1, group 2.” A person looks at what each group has in common and decides whether it means anything.

Going deeper · step 1

No labels, only measurements

In supervised learning every example comes with the answer. In unsupervised learning nobody supplies answers.

Most of the models on this route learn from labeled examples: photos marked “cat” or “dog,” emails marked “spam” or “not spam.” That is supervised learning. Often, though, you only have the measurements (the features) and no labels at all: thousands of songs with their tempo and energy, or shop visits with what was bought. Labeling them by hand would take too long, or nobody knows the right categories yet.

Unsupervised learning looks for structure in data like that. Two jobs are common. Clustering puts similar examples into groups. Dimensionality reduction squashes many measurements into a few, for example to draw a map. Both are useful for exploring, and both need a person to ask, “What did it actually find?”

Supervised learning: points that each carry a label, so a model can learn a rule that predicts the label. SUPERVISED every example comes with an answer “calm” “calm” “dance” “dance” “calm” learn a rule that predicts the answer Unsupervised learning: the same kind of points with no labels; a clustering method draws two groups, and a question mark asks what each group means. UNSUPERVISED only measurements, no answers group 1 = ? group 2 = ? find groups; a person decides what they mean
Made-up illustration. Without labels the computer can still draw the two groups, but it cannot tell you that one is “calm songs.” That name is a guess a person makes and then checks.

Going deeper · steps 2 to 4

k-means: assign, move, repeat

The most famous clustering method needs only distances and averages.

In k-means, you choose k, the number of groups you want. The computer places k centers (starting points), then repeats two steps:

1. Assign. Every example joins the center nearest to it. 2. Move. Every center moves to the average position of the examples that joined it. Then assign again, and so on, until no example changes group.

To score the groups, k-means uses the spread: for every example, take the distance to its center, square it, and add up all the squares. (scikit-learn calls this number inertia, or the within-cluster sum of squares.) Smaller spread means tighter groups. Each assign step and each move step can only lower the spread or leave it the same, so k-means always finishes. But it can finish in a worse place than the best possible one, depending on where the centers started. Real tools therefore run k-means several times from different starts and keep the run with the lowest spread.

Predict first

Song R sits at (3, 3). The centers are at C1 = (2, 4) and C2 = (6, 4). Which center does R join, and what is its squared distance?

Show the answer

R joins C1. To C1: (3 − 2)² + (3 − 4)² = 1 + 1 = 2. To C2: (3 − 6)² + (3 − 4)² = 9 + 1 = 10. You do not need the square root to compare distances: the nearer center always has the smaller sum of squares.

Worked example

One round of k-means with six made-up songs

  1. DataSix songs, each measured as (tempo, energy) on a 0–10 scale: P (1, 2), Q (2, 1), R (3, 3), S (7, 8), T (8, 6), U (9, 7). We pick k = 2 and start the centers at C1 = (2, 4) and C2 = (6, 4).
  2. DistanceFor each song, square the difference in tempo, square the difference in energy, and add. P to C1: (1 − 2)² + (2 − 4)² = 1 + 4 = 5. P to C2: (1 − 6)² + (2 − 4)² = 25 + 4 = 29. P is nearer C1.
  3. 1 · AssignQ: 9 to C1, 25 to C2. R: 2 to C1, 10 to C2. S: 41 to C1, 17 to C2. T: 40 to C1, 8 to C2. U: 58 to C1, 18 to C2. So P, Q, R join C1 and S, T, U join C2. Spread = 5 + 9 + 2 + 17 + 8 + 18 = 59.
  4. 2 · MoveC1 moves to the average of P, Q, R: tempo (1 + 2 + 3) ÷ 3 = 2, energy (2 + 1 + 3) ÷ 3 = 2, so (2, 2). C2 moves to the average of S, T, U: (7 + 8 + 9) ÷ 3 = 8 and (8 + 6 + 7) ÷ 3 = 7, so (8, 7).
  5. New spreadP 1, Q 1, R 2, S 2, T 1, U 1. Spread = 8, down from 59.
  6. Stop?Assign again: every song is still nearest its own center (R is 2 from C1 but 25 + 16 = 41 from C2). No song changed group, so k-means stops.

All six songs are made up. A person still has to look at the result and decide whether “group C1” means anything, such as “slow, calm songs.”

3 · Try it

Cluster fourteen songs

Each song has two made-up measurements: tempo (slow to fast) and energy (calm to intense).

Predict first

With k = 3, will every starting position for the centers end with the same three groups?

Show the answer

No. “Along the diagonal” and “Down the left side” both finish with a spread of 43. “Along the bottom” gets stuck at 77.71: two groups of songs end up sharing one center, because no single step could improve things. k-means always stops, but not always at the best answer. Try it in the lab.

Lab

Step through k-means

Pick k and a set of starting centers. Press Step to do one move at a time: first every song joins its nearest center, then every center moves to the average of its songs. Watch the spread, then compare starts.

Simulated in your browser with 14 made-up songs. Fixed starting centers, no randomness.

Number of groups (k)
Spread now–
Steps taken0

Groups

    What happened, step by step

      Move a song (changes the data)

      Moving a song changes the measurements, so the spread can jump up. Then press Step and watch k-means react.

      Finished runs (to compare starts and values of k)

      • None yet.

      The interactive version needs JavaScript. Here is the same lab as tables. Each run repeats two steps: every song joins its nearest center, then every center moves to the average of its songs. It stops when no song changes group. Look at the three runs for k = 3: two starts finish with a spread of 43, but “Along the bottom” gets stuck at 77.71.

      Fourteen made-up songs
      SongTempo (0 slow, 10 fast)Energy (0 calm, 10 intense)
      Song 121
      Song 288
      Song 355
      Song 418
      Song 532
      Song 698
      Song 746
      Song 829
      Song 912
      Song 1079
      Song 1164
      Song 1237
      Song 1323
      Song 1487
      Every run in the lab, worked out in advance
      RunStarting centersSpread after each stepFinal groups (song numbers)Final spread
      k = 2, Left and right(1, 5), (9, 5)158 → 101.78 → 101.45 → 971, 3, 4, 5, 7, 8, 9, 11, 12, 13 | 2, 6, 10, 1497
      k = 2, Bottom-left and top-right(1, 1), (9, 9)262 → 105.161, 3, 4, 5, 7, 9, 11, 12, 13 | 2, 6, 8, 10, 14105.16
      k = 2, Top-left and bottom-right(1, 9), (9, 1)422 → 178.4 → 172.73 → 165.14 → 155.71 → 130.29 → 110.98 → 104.67 → 104.24 → 101.78 → 101.45 → 971, 3, 4, 5, 7, 8, 9, 11, 12, 13 | 2, 6, 10, 1497
      k = 3, Along the diagonal(1, 1), (5, 5), (9, 9)86 → 431, 5, 9, 13 | 3, 4, 7, 8, 11, 12 | 2, 6, 10, 1443
      k = 3, Down the left side(1, 1), (1, 5), (1, 9)278 → 80 → 79.44 → 75.67 → 70.78 → 62.4 → 50.96 → 431, 5, 9, 13 | 3, 4, 7, 8, 11, 12 | 2, 6, 10, 1443
      k = 3, Along the bottom(1, 1), (5, 1), (9, 1)422 → 90.05 → 82.35 → 77.711, 4, 5, 8, 9, 12, 13 | 3, 7, 11 | 2, 6, 10, 1477.71
      k = 4, Along the diagonal(0, 0), (3, 3), (6, 6), (9, 9)98 → 65 → 51.28 → 44.47 → 39.74 → 32.8 → 22.96 → 19 → 19 → 161, 5, 9, 13 | 4, 8, 12 | 3, 7, 11 | 2, 6, 10, 1416
      k = 4, Four corners(1, 1), (1, 9), (9, 1), (9, 9)102 → 32.4 → 22.88 → 191, 5, 9, 13 | 4, 7, 8, 12 | 3, 11 | 2, 6, 10, 1419
      k = 4, Along the bottom(1, 1), (4, 1), (7, 1), (10, 1)413 → 87.9 → 81.54 → 61.4 → 49.96 → 41.671, 5, 9, 13 | 3, 4, 7, 8, 11, 12 | 2, 10, 14 | 641.67

      The human part

      A cluster is not yet a category

      k-means gives you “group 1, group 2, group 3.” Turning that into knowledge is a separate job.

      After a run you might look at the songs in one group and see that they are all fast and intense. You could call it “workout songs.” That name is your interpretation, not something the computer found. Maybe those songs are fast because they all came from one playlist a friend made. To trust a name, you check: listen to the songs, look at what else they have in common, and see whether new songs fit.

      The groups also depend on choices people made before the computer ran. Which measurements? Cluster the same songs by length and release year and you get completely different groups. How are they scaled? If tempo were in beats per minute (60 to 180) and energy on a 0–10 scale, tempo would swamp the distances, so tools usually rescale features first. Which k? In the best k = 3 run, songs 3, 7 and 11, which sit in the middle of the chart, join the slow, intense songs; with k = 4 they get a group of their own. Neither answer is “the truth.” Which one is useful depends on what you want to do.

      Predict first

      In the lab, the best spread for k = 2 is 97, for k = 3 it is 43, and for k = 4 it is 16. Should you always pick the k with the smallest spread?

      Show the answer

      No. More groups always allow a smaller spread. With 14 groups, one per song, the spread would be 0, and the “groups” would tell you nothing. People look for the point where adding a group stops helping much (here the drop is 54 from k = 2 to 3, then 27 from 3 to 4), and above all they ask whether the groups make sense and are useful.

      Dimensionality reduction

      Squash many measurements into a map

      Our songs had two measurements, so they fit on a flat chart. Real data rarely does.

      A music service might describe each song with a dozen numbers: tempo, energy, loudness, how acoustic it sounds, how much talking it has, its length, and more. Nobody can draw a chart with twelve directions. Dimensionality reduction squashes many measurements into a few, often two, so you can draw a map where similar songs land near each other. It can also make clustering faster and less confused by measurements that hardly matter.

      Think of a shadow. A shadow of your hand keeps its outline but loses its depth, and two different objects can cast the same shadow. Squashing data works the same way: something is always lost. Two points that are far apart in twelve directions can land close together on the map.

      For older students: one classic method, principal component analysis (PCA), finds the direction in which the data is most spread out and makes it the map’s first axis. The second axis is the next most spread-out direction at a right angle to the first. Whatever spread lies in the other directions is dropped.

      You have seen a map like this already. Language models store every word piece as a long list of numbers called an embedding, and the meaning map in “Turn words into places on a map” shows words in just two directions. That map is a simplified illustration. Maps made from real embeddings use dimensionality reduction, and they carry the same warning: closeness on the map is a hint, not proof.

      Predict first

      On a 2D map made from twelve song measurements, two songs land on exactly the same spot. Are they the same kind of song?

      Show the answer

      Not necessarily. They match in the two directions the map kept, but they may differ a lot in the directions that were squashed away, for example one is acoustic and one is electronic. To be sure, you go back to the original measurements.

      4 · Common mix-ups

      Groups are easy to read too much into

      Four ideas that trip people up, and what to think instead.

      • Mix-up: The computer discovered the true categories in the data.

        Better: It found groups that are close in the measurements people chose, for the k they chose, from the start it was given. Whether a group is a real category is a separate question that people check.

      • Mix-up: Unsupervised means no humans are involved.

        Better: People choose the data, the measurements, the scaling and k, and they interpret and name the groups. Only the labels are missing.

      • Mix-up: The smallest spread is always the best clustering.

        Better: More groups always lower the spread. One group per example gives a spread of 0 and no insight.

      • Mix-up: A 2D map of data shows the real distances.

        Better: A squashed map loses information. Points that look close may be far apart in the original measurements.

      5 · Check yourself

      Three quick questions

      Answer in your head or on paper before you open each one.

      1. A song is at (5, 5). Two centers are at (2, 2) and (8, 7). Which center does the song join? Show the squared distances.

        Check your answer

        To (2, 2): (5 − 2)² + (5 − 2)² = 9 + 9 = 18. To (8, 7): (5 − 8)² + (5 − 7)² = 9 + 4 = 13. The song joins (8, 7), because 13 is smaller.

      2. Two classmates run k-means with k = 3 on the same songs. One ends with a spread of 43 and the other with 77.71. Did one of them make a mistake?

        Check your answer

        Not necessarily. They probably started the centers in different places. k-means always finishes, but it can get stuck in a worse arrangement. The usual fix is to run it from several starts and keep the lowest spread, then still check whether the groups make sense.

      3. A shop clusters its customers into four groups and calls group 3 “bargain hunters.” What should you ask before anyone uses that name?

        Check your answer

        Which measurements were used, and do the people in group 3 really share that trait, or is the name a guess? Does the group reflect how the data was collected? And how will the label be used: could it lead to treating some people unfairly? A cluster is a group of numbers until a person checks what it means.

      In the real world

      Where finding groups helps, and where it misleads

      Application

      Grouping songs or customers

      Music and shopping services group items or shoppers with similar patterns, then a person decides what each group is for, such as a playlist theme or a newsletter topic.

      Application

      Spotting the odd one out

      An example that is far from every group center can be flagged for a person to look at: a strange sensor reading on a machine, or an unusual payment. Many flags turn out to be false alarms, so a human checks.

      Application

      Organizing photos

      Photo apps can put similar-looking pictures together so you can sort them faster. You still decide which groups to keep and what to call them.

      Limitation: groups can mirror how the data was collected, and can harden stereotypes. Photos may cluster by camera or lighting instead of by what is in them. When the examples are people, a cluster such as “customers from these neighborhoods” can turn into a label like “low-value customers” and then into decisions about real people, even though the group only reflects the measurements someone chose. Name groups carefully, check them, and ask who could be treated unfairly. See What did we teach it by accident?

      Connection: once people have checked and named the groups, the names can become labels for supervised models, which learn to sort new examples. And “far from every center” alarms need the same care as any alarm: count the false alarms, as on Good at what, for whom, compared with what?

      For younger learners

      A grades 3–5 version

      No numbers needed: the idea that the sorting rule makes the groups.

      20 minutes · teacher-led

      Sort without labels

      1. Give each table the same 12 picture cards (animals, vehicles, foods) with no category names.
      2. Ask each table to make three groups any way they like, and to write down their rule.
      3. Compare tables. One sorted by color, one by size, one by “things that move.” All are real groups.

      Exit question

      Who was right?

      Good answers say that every table found real groups, and that the groups came from what each table chose to look at. That is exactly why a computer’s groups need a person to check what they mean.

      Honest limits

      What this page leaves out

      What this page leaves out

      Many other clustering methods exist: some build a family tree of groups (hierarchical clustering), some find groups of any shape by looking for crowded regions (density-based clustering), and some give each example a probability of belonging to each group. Better ways to pick starting centers (such as k-means++), other map-making methods (t-SNE, UMAP) and learning without labels inside neural networks (self-supervised learning) are also left out. The scikit-learn guide in the sources lists them.