Unsupervised learning looks for patterns when no column contains the right answer.
That can reveal useful groups and simpler views. It cannot tell you what a group means, whether it is fair, or what action to take.
Lesson details
- Module
- Classical machine learning
- Lesson
- 5 · Unsupervised learning
- Difficulty
- Beginner
- Learning time
- 65 minutes
Before you start
- Features, vectors, distance, scaling, validation, and data leakage
- Supervised learning and why test evidence must stay separate
Skills you will gain
- Prepare an unlabeled dataset for clustering without leaking future information
- Explain and trace k-means assignments and centroid updates
- Compare k-means, hierarchical clustering, and DBSCAN
- Choose evidence for a cluster count without treating it as truth
- Explain PCA as a fitted linear projection and interpret explained variance
- Use t-SNE and UMAP maps carefully for exploration rather than measurement
Why this matters
Real datasets often arrive without an answer column. A support team may not have ticket topics. A product team may want to explore behaviour patterns. An engineer may need a compact representation before a downstream model.
Unsupervised learningUnsupervised learningFinding structure in examples that do not include a desired target answer, such as groups, compressed representations, or unusual cases. The discovered structure still needs human interpretation and evaluation. helps explore that structure. The responsible next step is investigation and evaluation, not automatically naming a discovered group “a customer type”.
Plain-English mental model
Step 1 — Know what problem you have
Use unsupervised methods when no reliable target answer is available during fitting.
- Clustering: propose groups of similar rows.
- Dimensionality reduction: represent many columns with fewer coordinates.
- Anomaly detection: find unusual rows under a fitted normality rule.
- Supervised prediction: learn a known target; this is different.
Clusters are not labels. If you manually label clusters and later predict that label, you have created a supervised task with all the usual split, leakage, and fairness requirements.
Step 2 — Prepare the representation before searching for groups
Distance-based methods treat numeric units literally.
If one feature is yearly spend in pounds and another is number of visits, raw spend can dominate Euclidean distance. Fit scaling on the data allowed at fit time, then apply the frozen transform consistently.
Decide deliberately how to encode:
- numeric measurements;
- categories;
- missing values;
- dates and seasonality;
- text, images, or embeddings;
- sensitive features and proxies.
Do not silently include an outcome, future field, account identifier, or timestamp that leaks the answer you hope to investigate.
Step 3 — k-means: assign, average, repeat
k-meansk-meansA clustering algorithm that repeatedly assigns each example to its nearest centre and moves each centre to the mean of its assigned examples. You choose the number of clusters, k, before fitting. needs a chosen number of clusters, k.
- Start with
kcentroidsCentroidThe arithmetic mean position of the examples assigned to a cluster. In k-means it is the cluster's movable centre, and it may not be an actual example., which are centre positions. - Assign each row to its closest centroid.
- Move each centroid to the arithmetic mean of its assigned rows.
- Repeat until assignments or centres stop changing enough.
The objective is inertiaInertiaThe sum of squared distances from every example to its assigned k-means centroid. Lower inertia always improves as k grows, so it cannot choose k by itself.: the sum of squared distances to assigned centres. Squaring makes far points count more.
inertia = Σ ||row - assigned_centroid||²
Worked example — two small piles
Rows are positions on one line: 0, 1, 8, 9. Start centres at 0 and 9.
| Row | Distance to 0 | Distance to 9 | Assignment |
|---|---|---|---|
| 0 | 0 | 9 | left |
| 1 | 1 | 8 | left |
| 8 | 8 | 1 | right |
| 9 | 9 | 0 | right |
Update centres:
left centre = mean(0, 1) = 0.5
right centre = mean(8, 9) = 8.5
One more assignment gives the same groups. That is convergence for this start, not proof of globally best or meaningful clusters.
Expected output:
assignments: {0: [0.0, 1.0], 1: [8.0, 9.0]}
updated centres: [0.5, 8.5]
Important lines
min(..., key=lambda i: abs(...))- Chooses the index of the closest current centre. Real k-means usually compares multidimensional squared Euclidean distances.
groups[closest].append(point)- This is the assignment step. Every point is forced into one group.
sum(...) / len(...)- This moves each centre to its cluster mean. Empty clusters need a deliberate recovery policy in production.
k-means assumptions and limitations
k-means is a useful baseline when compact, roughly round groups with similar scale are plausible after sensible preprocessing.
It struggles with:
- long, curved, or nested shapes;
- very different densities and sizes;
- outliers, because means move toward them;
- categorical variables treated as arbitrary numbers;
- high-dimensional noisy data;
- a
kchosen only because a plot looked tidy.
Use multiple starts. Its objective has local optima: different initial centroids can lead to different final partitions.
Step 4 — Select k using evidence, not certainty
Inertia always falls as k increases. At k = number of rows, every row can get its own centroid and inertia is zero. So inertia alone cannot select a useful count.
The elbow heuristic looks for diminishing improvement. It is a prompt to investigate, not a mathematical answer.
A silhouette scoreSilhouette scoreA cluster-quality heuristic comparing how close an example is to its own cluster with how close it is to the nearest other cluster. It is useful evidence, not proof that clusters are meaningful. compares within-cluster closeness to nearest-other-cluster closeness. Higher can suggest better separated geometry, but it also inherits your metric and representation.
Better evidence combines:
- stability across seeds, samples, and nearby parameters;
- inspection of representative rows and feature summaries;
- domain review of whether the distinction can support a real decision;
- a held-out downstream task, when clustering is used as a feature or workflow;
- monitoring for drift after deployment.
Step 5 — Hierarchical clustering keeps the alternatives
Hierarchical clusteringHierarchical clusteringClustering that creates a tree of nested groups by repeatedly merging nearby groups or splitting a large group. A chosen cut through the tree determines the final groups. creates a dendrogram, a tree of nested merges.
Agglomerative clustering starts with one row per cluster, then repeatedly merges the closest clusters. A horizontal cut at one height gives a small number of broad groups; a lower cut gives more groups.
The outcome depends on a linkage rule:
- single linkage: nearest pair across two groups; can chain;
- complete linkage: farthest pair; favours compact groups;
- average linkage: average cross-group distance;
- Ward linkage: merges that increase within-group variance least, usually with Euclidean distance.
It is valuable for exploration because it does not require choosing a final count before fitting. It can be memory- and time-intensive for large row counts.
Step 6 — DBSCAN finds density and permits noise
DBSCANDBSCANDensity-Based Spatial Clustering of Applications with Noise, a clustering method that connects dense regions and can mark isolated examples as noise rather than forcing them into a cluster. means Density-Based Spatial Clustering of Applications with Noise.
It needs two important settings:
eps: radius considered close;min_samples: enough nearby points to form a dense core.
Core points connect into dense clusters. Border points can join a core. Isolated points become noise label -1 rather than being forced into a group.
This can discover irregular shapes and handle outliers better than k-means. It is highly sensitive to feature scale and eps, and one global density setting struggles when real groups have very different densities.
Step 7 — PCA makes a smaller linear coordinate system
PCAPrincipal component analysisA linear dimensionality-reduction method that projects data onto new orthogonal directions ordered by how much variation in the fitted data they explain. finds new orthogonal axes:
- first principal component: direction with most fitted-data variation;
- second: next most variation, perpendicular to the first;
- later components: remaining independent directions.
Projecting from many columns to two components makes a plot. Keeping enough components can compress data before a downstream model.
Explained varianceExplained varianceThe proportion of variation in fitted data captured by one or more principal components. It measures reconstruction-oriented variation, not business usefulness or causality. is how much fitted-data variation retained components capture. It does not mean feature importance, prediction quality, causal importance, or business value.
PCA workflow
- Split first when a later supervised evaluation exists.
- Fit imputation and scaling on training rows only.
- Fit PCA on those transformed training rows.
- Transform validation, test, and future rows with the frozen fit.
- Inspect reconstruction, explained variance, stability, and downstream effect.
PCA can be distorted by unscaled units and outliers. Component signs can flip with no change in meaning. A component mixes original features, so explain it by inspecting loadings and examples, cautiously.
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
pipeline = Pipeline([
("scale", StandardScaler()),
("pca", PCA(n_components=0.90, random_state=0)),
("cluster", KMeans(n_clusters=4, n_init=20, random_state=0)),
])
pipeline.fit(training_rows)
cluster_ids = pipeline.predict(new_rows)
print(pipeline.named_steps["pca"].n_components_)
print(cluster_ids.tolist())Expected output has a data-dependent number of retained components and one cluster ID per new row:
3
[1, 0, 1, 3]
The code is copyable but needs installed scikit-learn and a real numeric table. n_components=0.90 asks PCA to retain at least 90% of the training variance; it does not promise 90% task performance.
Step 8 — Use t-SNE and UMAP as maps, not rulers
t-SNEt-SNEt-distributed Stochastic Neighbour Embedding, a nonlinear method for visualising local neighbourhoods in two or three dimensions. Global distances, cluster size, and blank space in its map are not reliable measurements. and UMAPUMAPUniform Manifold Approximation and Projection, a nonlinear dimensionality-reduction method often used for visual exploration and embeddings. Its output depends on choices such as neighbours, minimum distance, randomness, and preprocessing. make nonlinear low-dimensional views. They are often useful for exploring embedding neighbourhoods, checking possible label problems, or deciding what to inspect next.
They can mislead when treated as an objective map:
- local neighbours are more trustworthy than long-range distance;
- island size and empty space are not counts or density measurements;
- different seeds and parameters can change appearance;
- preprocessing and distance metric matter;
- plots do not validate a downstream classifier.
For t-SNE, perplexity controls a neighbourhood scale. For UMAP, n_neighbors and min_dist influence the local-to-broader structure trade-off and point compactness. Run more than one setting; compare to original-space neighbours and known metadata.
Worked system example — exploring support tickets responsibly
A support team has 200,000 ticket embeddings and no reliable topic label.
- Remove identifiers and protect raw text access.
- Sample a time slice and compute documented embeddings.
- Inspect nearest neighbours for a small sample first.
- Compare PCA for compression and UMAP only as an exploration view.
- Cluster candidate representations with several seeds and settings.
- Summarise clusters using representative, access-controlled tickets.
- Ask support specialists whether a group suggests a useful workflow.
- Validate an actual action: for example, whether a proposed routing rule reduces hand-offs on a later period.
The cluster is an investigation tool. The deployment decision needs an explicit outcome and evaluation.
Common mistakes
Calling clusters ground-truth customer types.
Fix — Treat them as hypotheses. Inspect examples, test stability, seek domain review, and validate the intended action separately.
Using raw mixed-scale features with Euclidean k-means or DBSCAN.
Fix — Choose a representation and fit scaling on allowed training or historical data; document units and transforms.
Choosing k from an elbow plot alone.
Fix — Combine geometry scores with stability, representative-row review, and downstream usefulness.
Assuming every point must belong to a meaningful cluster.
Fix — Allow noise, small groups, ambiguity, and an explicit no-action outcome. DBSCAN can label sparse points as noise.
Fitting PCA before a train/test split for a later prediction task.
Fix — Split first, then fit scaling and PCA only on training rows and transform held-out rows with that frozen fit.
Reading t-SNE or UMAP island size and spacing as measured truth.
Fix — Use maps to generate inspection questions. Check original-space neighbours, counts, parameters, seeds, and task evidence.
Forgetting time drift.
Fix — Evaluate cluster stability across time and avoid letting future behaviour define an earlier cohort.
Exercises
Points `(0,0)`, `(1,0)`, `(8,0)`, `(9,0)` start with centroids `(0,0)` and `(9,0)`. Assign each point to a nearest centroid, then calculate both updated centroids.
Reveal solution
The first two points join the left centre and the last two join the right centre. Their means are (0.5, 0) and (8.5, 0). If assignment no longer changes after another update, k-means has converged to this local solution.
A DBSCAN run labels 18% of orders as noise. Give three checks before deciding these are anomalous orders.
Reveal solution
Check units and scaling, inspect the epsilon and minimum-samples sensitivity, and sample records with domain experts. Also compare time periods, missing-value patterns, and a held-out downstream task. Noise is a density result, not automatically fraud or a bad record.
A two-dimensional t-SNE map has one large island and three small islands. Explain why you cannot conclude that the large island is more common or farther from the others than the small islands.
Reveal solution
t-SNE prioritises local-neighbourhood similarity. Its apparent area, empty space, and long-range distances are not faithful global measurements. Check original counts separately and inspect neighbour preservation, parameters, seeds, and the original feature space.
Knowledge check
Knowledge check
Practice activity
Practice · 10–20 minutes
Cluster a tiny dataset, then challenge the story
Goal: Perform one k-means update and practise separating a geometric grouping from an operational claim.
- Run the starter code and write down the two centroids after one update.
- Run a second update and check whether assignments changed.
- Change the first point to `(0, 20)` and describe how an outlier moves a mean.
- Choose one feature-scaling step you would need for age and annual spend.
- Write one question for a domain expert before naming either group.
Expected result: The first update groups the first two and last two points, then moves their centres to `(0.5, 0.0)` and `(8.5, 0.0)`. The outlier demonstrates that means are sensitive to extreme values.
Optional challenge: Run several initial centre choices and explain why a stable-looking partition still needs a downstream usefulness check.
Reveal solution
from math import dist
points = [(0.0, 0.0), (1.0, 0.0), (8.0, 0.0), (9.0, 0.0)]
centres = [(0.0, 0.0), (9.0, 0.0)]
groups = {0: [], 1: []}
for point in points:
nearest = min(range(2), key=lambda i: dist(point, centres[i]))
groups[nearest].append(point)
updated = []
for index in range(2):
xs = [point[0] for point in groups[index]]
ys = [point[1] for point in groups[index]]
updated.append((sum(xs) / len(xs), sum(ys) / len(ys)))
print(groups)
print(updated)Expected output:
{0: [(0.0, 0.0), (1.0, 0.0)], 1: [(8.0, 0.0), (9.0, 0.0)]}
[(0.5, 0.0), (8.5, 0.0)]Reveal solution and reasoning
Each early point is closer to the left start and each late point is closer to
the right start. A centroid is a mean, so it moves halfway between each pair.
If (0, 20) replaces (0, 0), the left mean moves upward sharply even though
only one row changed. That is why outliers and scaling matter. A good domain
question is: “Do these feature patterns correspond to a distinct support need
that our team can serve differently and safely?”
Key takeaways
- Unsupervised methods propose structure; they do not provide an answer column or a causal explanation.
- Representation, scaling, metric, missing-data handling, and time boundaries define the groups an algorithm can see.
- k-means alternates assignment and mean updates, but assumes geometry that may not fit the data.
- Hierarchical clustering shows nested choices; DBSCAN can find dense shapes and label sparse rows as noise.
- PCA is a fitted linear projection; explained variance is not prediction accuracy or importance.
Flashcards
Flashcards
1 of 8Revision and interview questions
- Trace one iteration of k-means and explain its objective, assumptions, and local-optimum risk.
- Compare k-means, hierarchical clustering, and DBSCAN for shape, noise, parameters, scale, and computational trade-offs.
- Design evidence for selecting and validating a cluster solution without confusing it with ground truth.
- Explain a leakage-safe PCA workflow and distinguish explained variance from model usefulness.
- Explain why t-SNE and UMAP maps can generate hypotheses but cannot prove global separation or segment size.
One-page sketchnote summary
Print me · one-page revision sheet
Unsupervised learning
Main idea
Unsupervised learning searches for patterns in a chosen representation. Clusters and maps are starting points for inspection; trustworthy systems validate the later decision on protected, meaningful evidence.
Core terms
- Cluster — proposed similarity group
- Centroid — mean centre
- Inertia — squared within-group distance
- DBSCAN — dense groups + noise
- PCA — linear projection axes
- Explained variance — retained variation
One picture
Code pattern
assign = nearest_centroid(rows, centres)
centres = mean_each_group(assign)
components = fit_pca(training_rows)
map = transform(future_rows, components)Watch out
- Do not call clusters ground truth
- Do not skip scaling and representation choices
- Do not choose k from inertia alone
- Do not fit PCA across a protected test split
- Do not use t-SNE / UMAP as global maps
Remember
- Geometry starts with preprocessing
- k-means assigns then averages
- Density can leave noise unclustered
- PCA preserves variation, not answers
- Explore → inspect → validate action
Three quick questions
- What made two rows similar?
- Would this grouping survive another seed?
- What real decision would this improve?
Lesson checklist
0 of 8 completeResources
- docsscikit-learn — clusteringOfficial guide to k-means, hierarchical clustering, DBSCAN, evaluation, and practical comparisons.
- docsscikit-learn — decompositionOfficial guide to PCA, explained variance, scaling, and incremental decomposition.
- docsscikit-learn — manifold learningOfficial cautions and parameters for t-SNE and related nonlinear visualisation methods.
- docsUMAP documentationOfficial documentation for UMAP parameters, supervised variants, transforms, and reproducibility.