Machine Learning Slides 📂 Introduction · 23 of 25 46 min read

Unsupervised Learning: Finding Hidden Structure Without Labels

No labels, no teacher — just raw data and the goal of uncovering the structure inside it. This tutorial maps the whole field across three families: clustering (K-Means, DBSCAN, hierarchical — group what's alike), dimensionality reduction (PCA, t-SNE, autoencoders — compress what's complex), and anomaly detection (Isolation Forest — flag what doesn't belong). Covers choosing K with the elbow and silhouette, evaluation without ground truth, an end-to-end segmentation pipeline.

🧩

Unsupervised Learning

No labels, no teacher — just raw data and the goal of finding the hidden structure inside it. Group what's alike, compress what's complex, and flag what doesn't belong.
Clustering Dimensionality Reduction Anomaly Detection

Press Next → or use ← → arrow keys

Section 01

Learning Without A Teacher

The new librarian and 10,000 unlabelled books
A librarian is handed ten thousand books with no titles, no genres, no catalogue — nothing but the content inside each one. To make sense of the pile, they start noticing patterns: these books share a vocabulary, those read like poetry, this odd one matches nothing else. Slowly, an organization emerges from the books themselves.

Unsupervised learning works exactly like that librarian. With no labels to guide it, it discovers the natural structure hidden in the data — groupings, compressed patterns, and oddities — purely from the data's own shape.
💡
The Defining Feature: No Labels

Supervised learning is given the answers and learns to reproduce them. Unsupervised learning gets no answers at all — it must find the questions and the structure on its own, which is exactly why it's so useful when labels are expensive or don't exist.

Section 01 · Contrast

Supervised vs Unsupervised

AspectSupervised LearningUnsupervised Learning
DataLabelled (X → y)Unlabelled (X only)
GoalPredict the known targetDiscover hidden structure
Typical tasksClassification, regressionClustering, reduction, anomalies
FeedbackRight/wrong from labelsNo ground truth
ExampleSpam vs not-spam emailGroup customers by behaviour
🧭
Why It Matters

The vast majority of real-world data arrives without labels — clicks, transactions, sensor readings, images. Unsupervised learning is how you extract value from it before anyone has spent the time (and money) to label a thing.

Section 02 · Map

Three Families, Seven Workhorses

Unsupervised Learning CLUSTERING "which things belong together?" K-Means DBSCAN Hierarchical groups similar points into natural clusters REDUCTION "what is the essence?" PCA t-SNE Autoencoders compresses many features into a meaningful few ANOMALY "what doesn't belong?" Isolation Forest One-Class SVM Autoencoders flags outliers, fraud, faults & intrusions
🗂️
One Question Each

Clustering asks which things belong together. Dimensionality reduction asks what the essence of the data is. Anomaly detection asks what doesn't belong. Master these three questions and you've mapped the whole field.

Section 03 · Clustering

K-Means — The City Planner

Place K depots, then keep moving them
Imagine planning K distribution depots for a city. Start by dropping them anywhere, assign every house to its nearest depot, then move each depot to the centre of the houses it serves. Repeat — reassign, recentre — until the depots stop moving. That's K-Means: alternate between assigning points to the closest centroid and recomputing each centroid as its cluster's mean, until convergence.
Inertia (within-cluster sum of squares)
Σ ‖ xᵢ − μₖ ‖²
Total squared distance from each point to its centroid — K-Means minimizes this.
Centroid update
μₖ = (1/|Cₖ|) Σ xᵢ
Each centroid moves to the mean of the points currently assigned to it.
⚠️
Know Its Limits

K-Means is fast and simple but you must pre-specify K, it assumes round, equal-sized clusters, it's thrown off by outliers, and — being distance-based — it demands scaled features. Great baseline; wrong tool for oddly-shaped clusters.

Section 03 · Diagram

Points Snap To Their Nearest Centroid

★ = centroid (cluster mean) · colour = assigned cluster
🎯
Assign, Recentre, Repeat

Every point takes the colour of its nearest ★ centroid; every centroid then jumps to the middle of its coloured points. A few rounds of this and the clusters lock into place — no labels ever required.

Section 03 · Choosing K

How Many Clusters? Elbow & Silhouette

number of clusters K → inertia K2 K3 elbow → K=4
Elbow method
inertia vs K → find the bend
Where adding another cluster stops meaningfully cutting inertia.
Silhouette score
s = (b − a) / max(a, b)
a = mean intra-cluster dist, b = nearest-cluster dist. Range −1…+1; higher is better.
Section 03 · DBSCAN

DBSCAN — Follows Density, Not Circles

K-Means → splits wrong a straight cut mixes both rings ✗ DBSCAN → density contours each ring found correctly ✓
🔭
No K, Any Shape, Plus Noise

DBSCAN grows clusters outward from dense "core" points — any point with at least min_samples neighbours inside radius eps. It discovers the cluster count on its own, traces arbitrary shapes like these rings, and explicitly labels leftover points as noise (−1) — three things K-Means simply can't do.

Section 03 · Hierarchical

Hierarchical — The Family Tree

A B C D E F cut → 2 clusters
🌳
Every K, In One Tree

Hierarchical clustering starts with each point alone and repeatedly merges the two closest groups (Ward linkage minimizes the variance added at each merge). The result is a dendrogram — slice it low for many clusters, high for few. One fit encodes all values of K, no re-running required.

Section 03 · Compare

Which Clustering Algorithm?

PropertyK-MeansDBSCANHierarchical
Cluster countPre-specify KAutomaticCut after
Cluster shapesSpherical onlyAny shapeFlexible
Noise handlingNo — assigns allYes — label −1Partial
SpeedVery fastSlower (high-dim)Slow on big data
Key parametersn_clusterseps, min_sampleslinkage, cut height
🧭
Rule Of Thumb

Reach for K-Means as a fast baseline on large, roughly-round clusters; DBSCAN when shapes are irregular, K is unknown, or noise must be flagged; and hierarchical when you want the full merge history or a dendrogram to explore.

Section 04 · Reduction

Dimensionality Reduction — The Essence

🔦
PCA
linear · fast
Projects onto the axes of maximum variance. The workhorse for preprocessing, noise removal, and feature engineering — always scale first.
🗺️
t-SNE
non-linear · viz only
Turns high-dim distances into probabilities and preserves local neighbourhoods in 2-D/3-D. The gold standard for seeing clusters — not for pipelines.
🧠
Autoencoders
non-linear · learned
A neural encoder squeezes data through a bottleneck; the decoder rebuilds it. Learns compact latent codes for denoising and anomaly detection.
🚫
t-SNE Is For Eyes, Not Pipelines

t-SNE preserves local structure but distorts global distances, and it's stochastic and slow on big data. Use it to visualize and confirm clusters — never as features feeding a downstream model. For compression pipelines, use PCA or an autoencoder.

Section 05 · Anomalies

Isolation Forest — The Odd One Out

Normal point → many splits split normal deep path = hard to isolate Anomaly → isolated fast split anomaly only 2 splits to isolate short path = anomaly score high
🌲
Anomalies Are Easy To Isolate

Build random trees that keep splitting the data. A normal point sits deep in the crowd and needs many splits to fence off; an anomaly stands alone and gets isolated in just one or two. The shorter the average path, the higher the anomaly score. On a fraud test it caught 19 of 20 frauds — 95% recall — with no labels at all.

Section 06 · Cheat-Sheet

Seven Algorithms At A Glance

AlgorithmBest ForScale?Specify K?Noise
K-MeansFast baseline, spherical clustersYesYesNo
DBSCANIrregular shapes, unknown KYesNoYes
HierarchicalMerge history, dendrogramsRec.Cut afterPartial
PCALinear reduction, denoisingYesn_componentsPartial
t-SNE2-D/3-D visualization onlyYes2 or 3Partial
AutoencoderNon-linear reduction, denoisingYeslatent dimYes
Isolation ForestAnomaly / fraud detectionNocontaminationCore
📋
Notice The Pattern

Nearly everything distance- or variance-based needs scaling — the one exception is the tree-based Isolation Forest. Match the algorithm to the question: group, compress, or flag.

Section 07 · Evaluation

How Do You Score It With No Labels?

📏
Silhouette Score
How tight and well-separated the clusters are, from −1 to +1. Higher means points sit closer to their own cluster than the next.
📉
Inertia
Total within-cluster squared distance. Always falls as K rises — read it via the elbow, not on its own.
🧑‍💼
Domain Judgment
The real test: are the groups meaningful and actionable? A K=4 you can act on beats a K=8 with a better score that nobody can use.
🕳️
The Core Challenge: No Ground Truth

With no labels, there's no "accuracy" to check against. Metrics like silhouette and inertia measure compactness, not business value. Always pair the numbers with domain knowledge — and never trust cluster labels until you've profiled what each group actually contains.

Section 08 · Pipeline

End-To-End — Customer Segmentation

🛒 Five Steps From Raw Rows To Named Segments
1Load & inspect — 50,000 customers × 30 features; check missing values, outliers, types.
2Scale with StandardScaler — K-Means and PCA are distance/variance-sensitive.
3PCA to denoise — reduce to ~90% variance; fewer, cleaner dimensions speed clustering.
4Find K — elbow + silhouette across K = 2–12, balanced against business sense.
5Profile & name — per-segment means on the original features → VIP, At-Risk, Regular, Occasional.
🏷️
The Payoff Is In The Profiling

Clustering hands you group numbers; the value comes from step 5 — computing each segment's real-world averages (recency, spend, returns) and giving it a name a business can act on. A "VIP" segment with high spend and frequency is worth infinitely more than "cluster 0."

Section 08 · Applications

Where Unsupervised Learning Earns Its Keep

🛍️
Customer Segmentation
Cluster shoppers by behaviour to target marketing, pricing, and retention.
🎬
Recommendation
Group similar users or items to power "customers like you also bought…".
🚨
Fraud & Faults
Anomaly detection flags fraudulent transactions, intrusions, and machine faults.
🖼️
Image Compression
PCA and autoencoders shrink images to compact codes with little visible loss.
🧬
Pattern Discovery
Surface latent structure in genomics, text corpora, and sensor streams.
🔍
Data Exploration
t-SNE / UMAP maps reveal clusters and sub-groups before any modelling begins.
Section 09 · Golden Rules

Seven Rules For Unsupervised Learning

🏅 Unsupervised Learning, Distilled
1Scale before distance-based methods. K-Means, DBSCAN, hierarchical, PCA, t-SNE all need it — Isolation Forest doesn't.
2Never trust cluster labels alone. Profile each group on the original features before naming it.
3PCA before K-Means on high-dim data. It denoises, compresses, and beats the curse of dimensionality.
4t-SNE is for visualization only — never feed its output into a downstream model.
5Evaluate with domain knowledge, not just silhouette and inertia. Actionable beats "optimal."
6Set random_state on everything stochastic — K-Means, t-SNE, Isolation Forest — for reproducibility.
7Set contamination deliberately for anomaly detection — estimate it from domain knowledge, not 'auto'.
Wrap-Up

You've Mapped Unsupervised Learning

3Core families
7Workhorse algorithms
σ=1Scale first
K?Elbow + silhouette
−1DBSCAN noise label
seedReproducibility
🎯
The Through-Line

Unsupervised learning finds structure without labels through three questions — group it (clustering), compress it (dimensionality reduction), or flag it (anomaly detection). Scale your features, tune parameters honestly, judge results with domain knowledge, and fix your random seeds.

📚
Where To Go Next

Deep-dive each algorithm on its own — K-Means and the elbow, DBSCAN, PCA, t-SNE / UMAP, and Isolation Forest — then build the full customer-segmentation pipeline end to end on a real dataset.

🧩 End of tutorial · Press to review, or click Restart