Learning Without Labels

When the data has no answer column to learn from, a model can still find groups of examples that are more like each other than like the rest, which is called clustering, a kind of unsupervised learning, and a cluster is a pattern in the numbers you chose, not a fact about the people behind them.

Apprentice · AI Basics · Python · Updated

By

After this lesson you can

  • Tell supervised from unsupervised learning by whether the data has a label column, and name clustering as finding groups of similar examples
  • Explain what a cluster is and isn't, and name real uses of clustering such as customer groups and spotting odd examples

Key takeaways

  • Supervised learning learns from a label column, the answers. Unsupervised learning has no label and looks for patterns in the features alone.
  • Clustering is the main kind of unsupervised learning. It finds groups, called clusters, of examples that are closer to each other than to the rest.
  • A cluster is a pattern in the columns you chose. It isn't a real type of person, and there's no answer key to check it against.
  • Clustering is used for customer groups, for sorting things such as support tickets by how alike they are, and for spotting odd examples that fit no group.

The owner of the Cozy Corner Café wants to plan next season: a breakfast deal, maybe a weekend brunch menu, maybe a card that brings back people who rarely come in. To plan for the café's regulars, she needs to know what kinds of regulars it has. Nobody ever wrote "commuter" or "bruncher" on a loyalty card, though. Finding groups in data that has no answers written down is a job you'll meet whenever someone asks "what kinds of customers, readers, tickets or patients do we have?"

The example for this chapter is the café's 600 loyalty members in cafe_members.csv, our own made-up data. It comes with Exercise: Café Regulars, beside the seed script that made it, so you can run this chapter's code in that folder.

No answer column

Every model in this course so far learned from examples with their answers: home sales with their prices, emails marked spam or not. The answer column is the label, and a model that learns from labeled examples is doing supervised learning: the label supervises it, like an answer key in the back of a workbook.

Here's the café's data:

import pandas as pd

members = pd.read_csv("cafe_members.csv")
print(members.shape)
print(members.head())
(600, 5)
  member_id  visits_per_month  avg_spend  share_before_9am  share_with_food
0   CC-1001                 8       6.38              0.00             0.46
1   CC-1002                14       5.97              0.93             0.22
2   CC-1003                 8       5.93              0.17             0.20
3   CC-1004                21       5.27              0.85             0.08
4   CC-1005                20       5.46              0.94             0.00

Each member has four numbers: visits a month, the average spend per visit in dollars, the share of visits before 9 a.m. (0.93 means 93% of them), and the share of visits with food. There's no fifth column saying what kind of regular each one is. There's nothing to learn to guess, so there's no answer key and no y. Learning from the features alone is called unsupervised learning.

Groups you can see

With only two features you can look for groups yourself. Put each member on a chart, visits across the bottom and spend up the side, and the dots aren't spread evenly:

The 600 café members as gray dots, visits per month across the bottom from 1 to 28 and average spend up the side from $2.62 to $29.45, all the same color because no group is written in the data: the dots form four clumps, many visits with a small spend near 17 visits and $5.50, about 10 visits and $7, a few visits with a middling spend near 3 visits and $10, and a few visits with a big spend near 4 visits and $21

Four clumps show, and a person could circle them. But the café has four features, a chart can't show four directions at once, and circling clumps by eye is a guess nobody else can repeat. Clustering does it with arithmetic: it finds groups of examples that are closer to each other than to the rest, measured with the straight-line distance from Distance Between Examples. Each group it finds is a cluster. Clustering is the main kind of unsupervised learning, and this chapter's method, k-means, is the classic way to do it.

What a cluster is, and what it isn't

A cluster is a set of members whose numbers sit close together. That's all it is. It isn't a real type of person: a member who visits 20 times a month, mostly before 9, is in the "early, often" cluster because of four numbers, not because anyone asked what their mornings are like. Choose other columns, say the drinks they order, and you'd get other groups from the same people.

There's also no answer key. A spam model's guesses can be checked against the emails' real labels; a clustering can't be marked right or wrong that way. scikit-learn's guide puts it plainly: in real data there's no single true number of clusters, and how many to look for is decided from the data and from what the groups are for. The café's data is the exception: our seed script planted four hidden groups (they aren't in the file), so this chapter can show whether k-means finds them. With real data, that's exactly what you can't know.

And a cluster describes the data; it doesn't explain it. If the "early, often" members spend little, the grouping doesn't say why. They may be in a hurry, or the café may sell little they want at that hour.

Where clustering is used

  • Customer groups: a store or a café finds kinds of shoppers and plans an offer for each, as this chapter does.
  • Sorting by likeness: support tickets, news stories or songs grouped by how alike they are, so a team can read one pile at a time.
  • Spotting odd ones out: an example far from every group, such as a payment unlike any other, is worth a second look.
  • A first look at new data: before building any model, clusters show what kinds of rows a table holds.

In each case a person still decides what the groups mean and what to do with them. Describing Each Group shows how.

Check yourself

2 questions to try on the lesson page.

So what

When there's no answer column, clustering can still find groups of similar examples, but each group is a pattern in the numbers you chose, not a fact about the people behind them.

Sources

  • scikit-learn, Glossary of Common Terms and API Elements: supervised learning is learning where the expected prediction (the label) is available for each sample when fitting; unsupervised learning is learning where it isn't, as in clusterers, and unsupervised estimators ignore any y passed to fit; a clusterer is an unsupervised predictor with a finite set of discrete output values.
  • scikit-learn 1.9.1, Clustering, User Guide: clustering of unlabeled data is done with the sklearn.cluster module, and k-means is one of its methods.
  • scikit-learn 1.9.1, Demonstration of k-means assumptions, example: in a real setting there is no uniquely defined true number of clusters; an appropriate number has to be decided from data-based criteria and knowledge of the intended goal.

Key terms

Related