Please enable JavaScript to use CodeHS

Machine learning · Unsupervised

What Is Clustering in Machine Learning?

How AI finds groups on its own

Nobody tells the computer what the groups are. It just looks at the data, notices which things sit close together, and sorts them. Learn how that works, then run the k-means algorithm yourself.

k-means ~14 min Middle / High school 4 interactives
The Big Idea

Labels or no labels?

Imagine someone dumps a giant bin of LEGO bricks on the floor and says, "Sort these." They don't tell you how. You might make a pile of red bricks, a pile of long bricks, a pile of wheels. You found the groups yourself, just by noticing which bricks are alike.

That's clustering. In machine learning (computers learning patterns from examples), there are two big ways a computer can learn:

Supervised learning

has labels · "here are the answers"

Every example comes with a label, the right answer, like a photo tagged "cat." The computer learns to give new things one of those known labels. Sorting new things into known labels is called classification.

Unsupervised learning

no labels · "find the pattern"

The examples have no labels. Nobody says what's right. The computer has to discover structure on its own. Clustering is the most common kind of unsupervised learning.

Clustering means sorting data into groups of similar things, called clusters, without being told what the groups are.

Why would anyone want that? Because labels are expensive. Someone has to sit down and tag every photo or song by hand. Most of the world's data, like millions of songs, shopping trips, or log-ins, has no labels at all. Clustering lets a computer find useful groups anyway.


How Computers See Similar

Data as dots: features and distance

A computer can't "listen" to a song and feel that it's chill. It needs numbers. So we describe each thing with a few measurements called features. For songs, two easy features are:

  • Tempo: how fast the beat is, from 0 (very slow) to 10 (very fast).
  • Energy: how loud and intense it feels, from 0 (calm) to 10 (wild).

With two features, every song becomes a dot on a graph: tempo goes across, energy goes up. Here's the big idea: dots that are close together are similar. The space between two dots is their distance, and small distance means "alike."

Fig. 2Song map: tempo vs. energy

Pick a song

Click any song

You'll see which songs are closest to it, and how far away they are.

Songs and numbers are made up for this example. Real music apps use dozens of features, but the idea is the same.

A tiny worked example: measuring distance

A right triangle from Lullaby Lights at (1, 1) to Sunday Pancakes at (5, 4). Across 4, up 3, distance 5. across 4 up 3 distance 5

Lullaby Lights is at (1, 1). Sunday Pancakes is at (5, 4).

To get from one to the other, go across 4 (5 − 1) and up 3 (4 − 1). Those make two sides of a right triangle. The straight-line distance is the long side: √(4×4 + 3×3) = √25 = 5.

You can also just measure it with a ruler. The computer uses this triangle rule (the Pythagorean theorem) because it's fast and exact.

Key idea: features turn things into dots. Distance between dots measures how alike things are. Clustering is just finding crowds of nearby dots.


The Algorithm

K-means, step by step

There are many clustering methods. The most famous is k-means. The "k" is the number of groups you want. "Means" is another word for averages, because the algorithm keeps finding the average spot of each group. (An algorithm is just a list of steps a computer follows.)

  1. Pick k. Decide how many groups you want, like k = 3.
  2. Place k centers. Drop k markers, called centers (or centroids), at random spots. They're just guesses.
  3. Assign. Every point joins the team of its nearest center.
  4. Move. Each center slides to the average position of the points on its team, right into the middle of its crowd.
  5. Repeat steps 3 and 4 until no point switches teams. The groups are now stable, and the algorithm stops.

The only math: an average

Say the chill group from the song map has four songs at (1, 1), (1, 3), (3, 1), and (3, 3). To find the new center, average the across numbers and the up numbers separately:

across: (1 + 1 + 3 + 3) ÷ 4 = 8 ÷ 4 = 2
up:     (1 + 3 + 1 + 3) ÷ 4 = 8 ÷ 4 = 2
new center = (2, 2)

That's it. Distance to pick teams, averages to move centers. Now you try being the computer.

Fig. 3Be the algorithm

Round 1, assign step. Click each point (or Tab to it and press Enter) to give it to the center you think is nearest. Each press cycles A, B, C. You can also press A, B, or C on your keyboard.

Hover over or focus a point to see its distance to each center.

Tip: dashed trails show where each center has been. The tricky point in the middle is worth checking with the distance readout.


Interactive

Try it: k-means playground

Now let the computer do the work on a lot more points. Pick a dataset, choose k, and press Step to go one move at a time, or Run to watch it finish. Click on the plot to add your own points. The shaded background shows which center owns each part of the map.

Fig. 4K-means playground
Data
k

Mouse or touch: click the plot to add a point. Keyboard: use "Add 10 random points."

Press Step to place 3 centers at random spots.

Choosing k: the elbow

K-means needs you to pick k, but how do you know how many groups there should be? One simple trick: run k-means with k = 1, 2, 3, and so on. Each time, add up how far every point is from its center. Call that the total distance. Smaller means tighter groups.

More centers always shrink the total (with one center per point, it would be zero, which is useless). So look for the spot where adding another center stops helping much. On the chart it looks like a bent arm, so it's called the elbow. This chart updates live from the playground's data.

Fig. 5Elbow chart for the playground data
Calculating...

Click a dot (or Tab to it and press Enter) to use that k in the playground. Try the Blobs data, then Random: random data has no clear elbow, because it has no real groups.


In The Real World

Where clustering is used

You've probably used clustering today without knowing it. Anywhere there's lots of unlabeled data, clustering helps find the shape of it.

Music playlists

Streaming apps describe songs with features like tempo, energy, and mood, then cluster them. Songs in the same cluster make a natural playlist, like "late-night chill."

Grouping customers

A store can cluster shoppers by what and when they buy. It might find "weekend bargain hunters" and "back-to-school parents," then plan sales for each group.

Sorting photos

Photo apps turn each face into a list of numbers, then cluster them. All the photos of your grandma land in one group, even though nobody labeled her.

Spotting unusual activity

Security teams cluster normal behavior, like the usual times and places students log in. A log-in that lands far from every cluster, say at 3 a.m. from a new country, gets flagged for a human to check. This is called anomaly detection.

Why anomaly detection works: attackers and broken machines don't usually behave like the crowd. If you know what normal looks like, "far from normal" is worth a second look. It isn't proof of anything bad, just a reason to check.


Be A Smart User

What clustering can't do

Clustering is powerful, but it's not magic. Here are three limits worth knowing. Each one can be seen in the playground above.

It doesn't know what the groups mean

K-means only says "these points go together." It outputs group 1, group 2, group 3. A human has to look inside and decide that group 2 is "workout songs." Sometimes a cluster means nothing useful at all. That's why the song map asks you to name the playlists.

The starting spots matter

Because the first centers are random, k-means can get stuck with a bad answer, like two centers crowded into one blob and one center stuck covering two blobs. Press New random start in the playground a few times and watch for it. Real tools run k-means many times and keep the best result.

Weird shapes confuse it

K-means assumes every group is a round blob around a center. Curvy shapes, like two moons or a ring inside a ring, get chopped by straight boundaries. Our eyes see two moons; k-means slices them down the middle. Other clustering methods, like DBSCAN, follow chains of nearby points instead and handle these shapes better.

One more: the features you choose decide what "similar" means. Cluster students by height and you get one set of groups. Cluster them by favorite subject and you get totally different ones. The computer only sees the numbers you give it.


Quick Answers

FAQ

What is clustering in machine learning?

Clustering is when a computer sorts data into groups of similar things without being told what the groups are. It looks at the numbers that describe each item, finds items that are close together, and puts them in the same group, called a cluster.

Is clustering supervised or unsupervised learning?

Clustering is unsupervised learning. The data has no labels, so nobody tells the computer the right answers. The computer has to discover the groups on its own by looking for items that are similar.

What is the difference between clustering and classification?

Classification is supervised: it learns from labeled examples, like photos marked cat or dog, and then sorts new items into those known labels. Clustering is unsupervised: there are no labels, so it invents its own groups, and a person decides later what each group means.

How does k-means clustering work?

You pick k, the number of groups. The algorithm places k centers, assigns every point to its nearest center, then moves each center to the average position of its points. It repeats the assign and move steps until no point changes groups.

How do you choose the number of clusters k?

A common trick is the elbow method. Run k-means for k equals 1, 2, 3, and so on, and add up how far every point is from its center. Plot those totals. The total drops quickly at first, then levels off. The bend, or elbow, is usually a good choice for k.

What is clustering used for in real life?

Music apps group songs that sound alike into playlists, stores group customers who shop alike, photo apps group pictures of the same face, and security teams use clusters of normal activity to spot unusual activity that could be a problem.


Check Yourself

Quick quiz

1. Why is clustering called "unsupervised" learning?

2. In k-means, what happens during the "move" step?

3. Center A is at (2, 2). Its group has points at (0, 2), (4, 2), and (2, 5). Where does A move?

4. A k-means run gives you "Cluster 1, Cluster 2, Cluster 3." What is still missing?

Keep learning

Clustering finds groups with no labels. Next, see how computers learn when they do have labels, how apps use similarity to recommend things, and how AI turns words into dots it can measure.