Unsupervised learning
- Lesson 3
- 50 min
- Module 1: Understand machine learning
Lesson 3 of 27 · Machine Learning: From Problem to Reliable ModelUnsupervised learning
1. Understand machine learning
2. Frame the prediction problem
3. Evaluate supervised models
4. Build the first models
5. Try other model families
6. Tune and finish the model
7. Understand and choose the model
8. Prepare the model for handoff
What you will be able to do
Understand what a program can learn from data that has no labels: groups of similar examples. Then you can use clustering to explore data. You also know why its result is a suggestion that a person must judge, not an answer that you can check.
NotebookDo this lesson in a notebook
The notebook sorts the colour swatches of the last lesson into piles without their names, and compares the piles with the names. Then it makes a first grouping of Larkfield’s customers. Its setup cells install pandas and scikit-learn and download the practice data, so it needs no local setup. About 20 minutes.
Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.
Before you startWhat to know and to have ready
What you need to know
- Features, labels,
Xandy, from Supervised learning: Features and labels. - The colour swatches as three numbers (red, green and blue light), from Supervised learning: A tiny example.
What you need on your computer (only for the local route; the notebook needs none of it)
- Nothing yet. You make the project folder
~/projects/ticket-modelin Module 2, in Define a baseline: Set up your project.
The notebook was tested in Jupyter on macOS. It used Python 3.14.6, pandas 3.0.6, scikit-learn 1.9.1 and matplotlib 3.11.2.
Sorting a box of buttons
Section titled “Sorting a box of buttons”Imagine a large box of mixed buttons, and someone asks you to sort them. Nobody tells you the right groups, and nobody has written names on the buttons. You start anyway: you put similar buttons together. Soon you have a pile of red buttons, a pile of black ones and a pile of white ones.
Your friend sorts the same box differently: large buttons and small buttons. A tailor sorts it by the number of holes, because that is what matters for sewing. Nobody is wrong. Each way of sorting finds a real pattern in the buttons, and the best one depends on what you want to do with the piles.
This is the second way that a program can learn. There are no right answers to learn from. The program looks for groups of similar examples on its own.
Learning without a supervisor
Section titled “Learning without a supervisor”In supervised learning, every example has a label: the right answer that a supervisor gave. But often there is no label. Labels may cost too much: nobody will read and label a million old emails. Or nobody knows the right answer yet: before a shop studies its customers, nobody can say which “kinds” of customer it has.
Without labels, a program can still find structure in the features: examples that are similar to each other, and different from the rest. This is unsupervised learning: learning from the features alone, with no supervisor that gives the right answers. The method gets X, and there is no y.
The most common unsupervised task is clustering: putting similar examples into groups, called clusters. You meet clustering in many places:
- A shop groups its customers by what and how often they buy, for example “frequent small orders” and “rare large orders”. Each group gets different offers. These groups are often called customer segments.
- A news site groups thousands of new articles by topic each day, without a person reading each one.
- A photo app groups photos of the same face, and then asks you for the person’s name.
- A help desk groups support requests that look alike, to find the most common kinds of problem.
Unsupervised learning has other uses too. Anomaly detection finds the few examples that are unlike all the others. An example is a card payment that does not fit a customer’s usual pattern. Dimensionality reduction summarises many columns in a few, for example to draw a picture of the data in two dimensions. Unsupervised exploration uses one such method, PCA.
A tiny example: the swatches without names
Section titled “A tiny example: the swatches without names”Remember the 15 colour swatches of the last lesson. Now take away the name cards. A program gets only the three numbers of each swatch. Can it still find the colour groups?
Priya uses k-means, the most common clustering method. You choose k, the number of clusters. The method then works like this:
- It places
kcentres among the examples. - It puts each example in the cluster of the nearest centre.
- It moves each centre to the middle of its cluster.
- It repeats steps 2 and 3 until nothing changes.
The notebook makes the swatches again, but keeps their names in a separate column hidden_names. The method sees only X. Priya uses the names only at the end, to compare. Priya asks for 5 clusters:
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=5, n_init=10, random_state=0)swatches["pile"] = kmeans.fit_predict(X) # no labels hereprint(swatches["pile"].tolist())The output:
[1, 1, 1, 4, 4, 4, 3, 3, 3, 2, 2, 2, 0, 0, 0]fit_predict(X) has no y: the method learns from the features alone. Each swatch gets a cluster number from 0 to 4. The rows are in the order of the table: three reds, three oranges, three yellows, three greens and three blues. So each of the five clusters has three swatches. The settings n_init=10 and random_state=0 make the result the same on every run.
Now Priya compares the clusters with the names that the method never saw. In the table, each row is a cluster and each column is a name:
pd.crosstab(swatches["pile"], swatches["hidden_names"])The output:
hidden_names blue green orange red yellowpile0 3 0 0 0 01 0 0 0 3 02 0 3 0 0 03 0 0 0 0 34 0 0 3 0 0Each cluster has exactly one colour name. The method found the same groups as a person, without a single label. But look at what it did not find: the names. Cluster 1 is only a number. A person must look at it and say “these are the reds”.
Then Priya asks for 2 clusters instead of 5. Now one cluster has all the red, orange and yellow swatches, and the other has the green and blue ones: warm colours and cool colours. This is another good way to sort the same box. The picture shows both results.
Text description of the diagram
On the left, the 15 swatches of the last lesson lie in a mixed heap. They have no name cards: nobody gave the right answers.
Two arrows lead to two ways of sorting. At the top, k-means with five clusters makes five piles of three: the reds, the oranges, the yellows, the greens and the blues. At the bottom, k-means with two clusters makes two piles. One has the nine red, orange and yellow swatches (warm colours). The other has the six green and blue swatches (cool colours). Both are correct sortings. Which one is useful depends on what you want to do with the piles.
Why a person must judge the groups
Section titled “Why a person must judge the groups”In supervised learning, you can check a model: compare its predictions with the labels, and count how often it is right. In clustering, there is nothing to compare with. There are no labels, so there is no “right” cluster for any example. Five piles or two piles? Both fit the data.
So “good clusters” means useful clusters: groups that help someone make a decision. A painter who mixes paints may want five piles. A designer who chooses a warm or a cool theme for a room may want two. Only a person who knows the decision can say which grouping helps. That person also gives each cluster its name, because the method gives only numbers.
Omar asks: “Can the computer not tell us the best number of clusters?” There are measures that help, and Unsupervised exploration shows two of them. But they only say how compact and separate the clusters are. They cannot say if a cluster means something for Larkfield. In this course, they are hints, and a person makes the choice.
Supervised and unsupervised side by side
Section titled “Supervised and unsupervised side by side”The two kinds of learning answer different questions. Supervised learning answers “what is the right answer for this new example?”, and it needs examples with that answer. Unsupervised learning answers “what groups or patterns are in this data?”, and it needs only the examples. You can measure supervised learning against the labels. You must judge unsupervised learning by its use. The table sums up the differences.
| Supervised learning | Unsupervised learning | |
|---|---|---|
| Everyday picture | A child learns colour names from a parent | You sort a box of mixed buttons |
| The data | Features and a label (X and y) |
Features only (X) |
| What it learns | A rule from features to the label | Groups or structure in the features |
| Main tasks | Classification, regression | Clustering (also anomaly detection, dimensionality reduction) |
| Its output for one example | A predicted label: “orange”, 261.67 | A cluster number: 4 |
| How you check it | Compare predictions with the true labels | A person judges if the groups are useful |
| Larkfield example | Will this ticket escalate? | What kinds of customers do we have? |
Larkfield: groups of customers
Section titled “Larkfield: groups of customers”Grace asks a question that no column answers: “What kinds of customers do we have?” There is no label, because nobody ever wrote down a “kind” for each customer. So this is a question for clustering.
Priya makes one row per customer from the train tickets, with two features. One is the number of tickets the customer wrote. The other is how long the customer has been with Larkfield (tenure_days, in days). Guest tickets have no customer ID, so she leaves them out:
train = pd.read_csv("data/train.csv", dtype={"customer_id": "string"}, keep_default_na=False, na_values=[""])known = train.dropna(subset=["customer_id"])customers = known.groupby("customer_id").agg( tickets=("ticket_id", "size"), tenure_days=("customer_tenure_days", "max"),)print(customers.shape)customers.head(3)The output:
(847, 2) tickets tenure_dayscustomer_idC-00001 62 2579.0C-00002 10 1688.0C-00003 2 277.0847 customers, two features and no label. Then Priya asks k-means for 3 clusters. One step comes first: StandardScaler puts both columns on the same scale. Without it, the days (up to about 2,600) would count much more than the tickets. Unsupervised exploration explains this step. The last lines describe each cluster by its size and its mean values:
from sklearn.pipeline import make_pipelinefrom sklearn.preprocessing import StandardScaler
grouper = make_pipeline(StandardScaler(), KMeans(n_clusters=3, n_init=10, random_state=0))customers["group"] = grouper.fit_predict(customers[["tickets", "tenure_days"]])customers.groupby("group").agg( customers=("tickets", "size"), tickets=("tickets", "mean"), tenure_days=("tenure_days", "mean")).round(1)The output:
customers tickets tenure_daysgroup0 401 15.7 1882.71 384 10.8 594.92 62 101.5 1528.2The method gives three numbers. Grace gives the names. Group 0 has 401 customers who have been with Larkfield for about five years: “long-time customers”. Group 1 has 384 customers of about a year and a half: “newer customers”. Group 2 has only 62 customers, but each one wrote about 100 tickets: “a few very busy accounts”.
Then Grace judges if the groups are useful. The busy accounts may need one account manager each, so that group helps a decision. Whether the other two groups need different treatment is a question for her team, not for the method. In Unsupervised exploration, Priya clusters the same customers with more columns, and Grace decides what to do with the groups.
Guided practice: sort the swatches
Section titled “Guided practice: sort the swatches”- Open the notebook from the box Do this lesson in a notebook at the top of this page.
- Run the setup cells. The data cell ends with
Ready: train 16939 rows (1983 escalated), valid 1179 (183), test 1002 (157). - Run section 1. Check that
Xhas 15 rows, 3 columns and no label. - Before you run section 2, predict: will the five clusters match the five colour names?
- Run section 2. Compare the cluster numbers and the table with the tiny example above.
- Run section 3. Find the five colours in the picture, each with its own cluster number.
- Write one sentence: what did k-means find, and what did a person have to add?
Show an example answer
k-means found five groups of three swatches, and each group is one colour. It did not find the names: the clusters are numbers from 0 to 4, and a person must say “cluster 1 is the reds”. In the picture, the clusters are easy to see, but green and blue are close together: the third feature, blue light, separates them.
Your turn: two piles instead of five
Section titled “Your turn: two piles instead of five”Change one thing: the number of clusters. In section 4 of the notebook, sort the swatches into 2 clusters with KMeans(n_clusters=2, n_init=10, random_state=0). Put the size of each cluster, from the largest to the smallest, in the list pile_sizes. Before you run it, predict which colours go together.
Show an example answer
swatches["pile2"] = KMeans(n_clusters=2, n_init=10, random_state=0).fit_predict(X)pile_sizes = sorted(swatches["pile2"].value_counts().tolist(), reverse=True)print(pile_sizes)[9, 6]The cluster of 9 has the red, orange and yellow swatches. The cluster of 6 has the green and blue ones. These are warm and cool colours. The data did not change, only k. Both groupings are real patterns. Which one to use depends on the decision.
Common mistake
A cluster number is not a name
Symptom: Tomás saves the five clusters, and writes in his notes: “cluster 1 = red”. His code sends every swatch in cluster 1 to the red paint shelf. The next day, he runs k-means again with another random_state:
again = KMeans(n_clusters=5, n_init=10, random_state=1).fit_predict(X)print("First run: ", swatches["pile"].tolist())print("Second run:", again.tolist())print("Reds are in pile", again[0], "now")The output:
First run: [1, 1, 1, 4, 4, 4, 3, 3, 3, 2, 2, 2, 0, 0, 0]Second run: [2, 2, 2, 4, 4, 4, 0, 0, 0, 1, 1, 1, 3, 3, 3]Reds are in pile 2 nowCluster 1 is now the greens, and they go to the red shelf.
Cause: the groups are the same, but their numbers changed. A cluster number is a random name tag. It depends on where the method placed its first centres, and a new run, new data or another setting can change it.
Fix: never write code or notes that depend on a cluster number. Describe each cluster by what is in it, for example its mean values or a few typical examples. Then let a person give it a name, and store that name with the description.
Consequence in AI work: when a clustering is run again on new data each month, the numbers change without warning. A report that says “cluster 2 grew by 40%” may compare two different groups. Teams that track clusters over time must match them by their content, not by their numbers.
Knowledge check
Section titled “Knowledge check”Check your understanding
Larkfield has 5,000 old support emails with no labels, and wants to find the main kinds of problems in them. Is this supervised or unsupervised learning? Which task is it, and who decides if the result is good?
Show the answer
Unsupervised learning: the emails have no labels. The task is clustering: put similar emails into groups. A person who knows the support work, such as Grace, must look at the groups, name them and decide if they are useful. For example, a group may point to a problem that a help page could solve. There is no right answer to compare with.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Tomás says: 'k-means found 4 clusters, so our customers are of 4 types.' What is wrong with this sentence?
Show the answer
k-means found 4 clusters because Tomás asked for 4: the number of clusters is a choice, not a discovery. With 3 or 5, it finds 3 or 5 groups. Each grouping is one possible way to sort the customers. Whether 4 groups are the right “types” depends on whether they are useful for a decision, and a person must judge that.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Why can you count how often a spam classifier is right, but not how often a clustering of customers is right?
Show the answer
A spam classifier is supervised: each test email has a label, so you can compare each prediction with it and count the correct ones. A clustering is unsupervised: no customer has a “right” cluster, so there is nothing to compare with. You can only judge the clusters by their use, for example whether a group needs a different kind of support.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Recap
- Unsupervised learning learns from the features alone (
X, noy), as you sort a box of buttons that nobody has labelled. - Clustering puts similar examples into groups, called clusters. k-means does it for a number of clusters
kthat you choose. Other unsupervised uses are anomaly detection and dimensionality reduction. - Without names, k-means sorted the 15 swatches into five piles that match the colour names. Asked for two, it made a warm pile and a cool pile. Both are real patterns.
- There is no right answer to compare with, so a person must judge the clusters by their use and give them names. A cluster number is a random tag, not a name.
- Larkfield’s 847 customers fell into three groups. Grace named them, and found one that needs action: 62 very busy accounts.
Next, you put the three tasks together and choose the right one, or no model at all, for a new question.
Next: Choose the learning task