Semester 1 · Week 14
Unsupervised learning
- Ch 19 Unsupervised Learning
Chapter 19: Unsupervised learning
No labels. What does "similar" mean here?
Notes · slide 2
Time: 50 minutes for one chapter. About 25 on the algorithms, 10 on judging and choosing, 10 on the worked example, and 5 to check understanding.
Say: Every model so far needed labels. Someone marked each example malignant or benign, fraud or not. Labels take time and money. Often there are none: which customers are alike? Does this log line look odd?
Key line: no labels. So everything depends on what "similar" means here.
Next: the two tasks.
Two tasks
- Clustering: group similar things without being told the groups. Customer segments, document groups.
- Anomaly detection: find what doesn't fit. Fraud on new accounts, intrusions, manufacturing defects.
Both depend on how you define distance or density.
Notes · slide 3
Say: Clustering groups similar things without being told the groups: customer segments, document groups. Anomaly detection finds what doesn't fit: fraud on new accounts before any fraud is labelled, network intrusions, manufacturing defects.
Key line: both rest on how you define distance or density. Change that, and you change the answer.
Ask: "Why can't we train a supervised fraud model on a brand-new product?" Listen for: no labelled fraud exists yet.
Next: k-means.
K-means
- Place K centroids at random.
- Assign each point to its nearest centroid.
- Move each centroid to the mean of its points.
- Repeat until nothing moves.
- Fast and simple. You choose K.
- Assumes compact, convex, similar-sized clusters.
- Use k-means++ initialisation and scale features first.
Notes · slide 4
Do: draw a scatter on the board with two blobs. Place two crosses at random, assign points to the nearest cross, move each cross to the middle of its points. Two rounds is enough.
Say: Fast, simple, and easy to explain. But you choose K, and it assumes compact, round, similar-sized clusters. It settles in a local minimum, so it's run from several starts.
Say: Sometimes K is obvious, like malignant or benign. Sometimes the business decides: 3 customer tiers or 300 micro-segments. The data can't tell you which.
Next: a softer version.
Gaussian mixture models
- Model the data as a mix of Gaussians, each with its own mean and covariance.
- Fit with expectation-maximisation.
- Soft assignments: each point gets a probability per cluster.
- Clusters can be elliptical, not only round.
Notes · slide 5
Say: A GMM treats the data as a mix of bell-shaped clusters, each with its own centre and shape. Clusters can be stretched or tilted, not just round.
Say: Assignments are soft. A point near a boundary can be 60% one cluster and 40% another. That's often a truer description of ambiguous data.
Say: It's fitted with the same assign-then-update loop as k-means, made probabilistic. BIC or AIC help choose the number of clusters.
Next: clusters of any shape.
DBSCAN
- No K needed. Clusters are dense regions separated by sparse ones.
eps: the neighbourhood radius.min_samples: neighbours needed to be a core point.- Points that belong nowhere are labelled noise (
-1). Free outlier detection. - Finds clusters of any shape. Struggles when densities differ.
Notes · slide 6
Say: DBSCAN needs no K. A cluster is a dense region separated by sparse space. Two settings: eps, the neighbourhood radius, and min_samples, how many neighbours make a core point.
Say: Points that belong to no dense region get the label −1: noise. That's a free outlier list.
Key line: it finds any shape. It struggles when clusters have very different densities.
Next: building a tree of clusters.
Hierarchical clustering
- Merge the closest clusters step by step. The result is a dendrogram.
- Cut it at any height for any number of clusters.
- Good for exploring when K is unknown.
- Cost grows at least quadratically. Impractical beyond ~10,000 rows.
Notes · slide 7
Say: Start with every point alone. Merge the two closest clusters. Repeat until one cluster remains. The record of merges is a dendrogram. Cut it at any height to get any number of clusters.
Say: Good for exploring when you don't know K. But time and memory grow at least with the square of the rows, so it's impractical past about 10,000.
Next: detecting anomalies directly.
Isolation forest
- Anomalies are few and different, so they're easy to isolate.
- Build trees that split on random features at random values.
- An anomaly ends up alone after only a few splits.
- Score: the average path length to isolation. Shorter means more anomalous.
Notes · slide 8
Say: Anomalies are few and different, so they're easy to separate. Build trees that split on a random feature at a random value. An anomaly ends up alone after a few splits. A typical point needs many.
Key line: the score is the average number of splits to isolate a point. Fewer means more anomalous.
Say: You set contamination, the expected share of anomalies. Take it from domain knowledge, not a guess.
Next: how do you know if clusters are any good?
Judging clusters without labels
- There's no ground truth to check against.
- Silhouette score and similar metrics measure compactness and separation.
- They don't measure business usefulness.
- Choose K with the elbow method and silhouette, then check the clusters make sense to someone who knows the domain.
Notes · slide 9
Say: There's no answer key. The silhouette score measures how well each point fits its own cluster compared with the next nearest. It measures geometry, not usefulness.
Key line: a mediocre silhouette can still be the most useful clustering, if a domain expert confirms the groups behave differently.
Lab: learners trust the silhouette score too much. The Dry Bean lab lets them check it against hidden labels.
Next: choosing an algorithm.
Choosing an algorithm
| Situation | Reach for |
|---|---|
| Round clusters, known K, large data | K-means |
| Overlapping or elliptical clusters | Gaussian mixture |
| Odd shapes, noise, unknown K | DBSCAN |
| Exploring structure, smaller data | Hierarchical |
| Finding outliers | Isolation forest |
Notes · slide 10
Say: Round clusters, known K, large data: k-means. Overlapping or elliptical: Gaussian mixture. Odd shapes, noise, unknown K: DBSCAN. Exploring smaller data: hierarchical. Finding outliers: isolation forest.
Say: Scale features first for all the distance-based ones, as with KNN in week 8.
Ask: "Segmenting a million customers into five tiers for a campaign?" Listen for: k-means.
Next: the worked example.
Worked example: two crescents
300 points in two interleaved moons. Adjusted Rand Index (1 = perfect):
| Algorithm | ARI |
|---|---|
| K-means | 0.479 |
| Gaussian mixture | 0.479 |
DBSCAN (eps = 0.3) |
0.987 |
K-means and GMM both assume each cluster is one convex blob. Crescents aren't.
Notes · slide 11
Say: 300 points in two interleaved moons. Score each method against the true labels with the Adjusted Rand Index: 1 is perfect, 0 is random.
Say: K-means scores 0.479. It always finds some split, but cuts across the crescents. The GMM, more flexible, scores exactly the same. DBSCAN scores 0.987.
Key line: the problem isn't roundness. It's that k-means and GMM both treat each cluster as one convex blob. A crescent isn't one.
Next: finding anomalies.
Worked example: finding anomalies
500 normal points plus 25 scattered outliers. Isolation forest, contamination = 0.05:
- Flags 27 points.
- Catches 23 of 25 real anomalies: recall 0.92.
- Precision 0.85. A few normal points sit far out by chance.
A realistic result, not a perfect one.
Notes · slide 12
Say: 500 normal points plus 25 outliers scattered wide. Isolation forest with contamination 0.05 flags 27 points. It catches 23 of the 25 real anomalies. Precision 0.85: a few normal points happened to land far out by chance.
Key line: a realistic result, not a perfect one. Even a well-set contamination doesn't give perfect precision and recall.
Lab: in the anomaly task, watch for anyone using the labels to fit. Labels are only for checking afterwards.
Next: the chapter in one idea.
Mental model
Clustering is map-making. The map isn't the territory.
- There's no single correct clustering, only useful ones.
- Each algorithm draws a different map by definition.
- When the map's assumptions don't match the data's shape, you get the moons result.
Notes · slide 13
Say: Clustering is map-making, and the map isn't the territory. There's no single correct clustering, only useful ones. Each algorithm draws a different map by definition.
Key line: when the map's assumptions don't match the data's shape, you get the moons result.
Next: check yourself.
Check yourself
- Why must you scale features before k-means?
- What does DBSCAN label
-1, and why is that useful? - A high silhouette score: does that make the clusters useful?
- Which algorithm would you try on GPS points of delivery stops, with noise?
Notes · slide 14
Do: pairs, three minutes, then take answers.
Answers: 1. Distances are dominated by the features with the biggest scales. 2. Noise points: a built-in outlier list. 3. Not necessarily. It measures geometry, not business value. 4. DBSCAN: irregular shapes and noise.
This week
- Read Chapter 19: Unsupervised Learning.
- Take the Chapter 19 quiz.
- Run the notebooks: Chapter 19 (opens in a new tab).
- Optional: the further reading for week 14.
Slide 1 of 15: Unsupervised learning
Keys: → or Space next · ← back · Home/End · F full screen · N notes. Swipe on touch screens. "Save as PDF" opens the print dialog with one slide per page: choose "Save as PDF" as the printer. "Print handout" adds the speaker notes, in black on white.