Unsupervised Learning

Find hidden groups in data that has no labels. By the end you'll run one K-Means step by hand, cluster with scikit-learn, choose K with the elbow and silhouette methods, reach for DBSCAN and hierarchical clustering, and know where association rules and anomaly detection fit in.

Learn Unsupervised Learning in our free AI & Machine Learning course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…

Part of the free AI & Machine Learning course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

🌍 Real-World Analogy: sorting without labels

Imagine someone hands you a shoebox of 1,000 photos with no folders, no tags, nothing . Nobody tells you the categories. Yet within minutes you make piles: beaches, faces, food, pets. You never knew the labels in advance — you discovered them by noticing which photos look alike.

That is unsupervised learning. Supervised learning is like sorting photos that already have a label on the back; unsupervised learning is sorting blank-backed photos into piles you invent yourself. The algorithm only sees the raw data and groups similar things together — which is exactly what clustering means.

This lesson focuses on clustering, then tours the other three at the end.

1 K-Means, Step by Step (the whole idea)

K-Means is the most popular clustering algorithm, and its whole engine is two steps repeated in a loop. You pick K (how many groups you want), drop K centroids (a centroid is just the centre point of a cluster), then:

Repeat assign → update until the centroids stop moving. That's it — no labels anywhere. The code below does one full iteration on six 1-D points in plain Python so you can see exactly what happens. Read every comment, then run it.

2 The Real Tool: scikit-learn's KMeans

You'll never hand-code the loop in real work — scikit-learn does it in two lines and runs the whole assign/update cycle for you. The same six points, now clustered by KMeans :

fit_predict(X) trains the model and hands back the cluster id of every point in one call. inertia_ is the total spread within clusters — smaller is tighter — and you'll use it for the elbow method in Section 5.

Your Turn 1: finish the assign step

One blank to fill. Each point must join whichever centre is nearer — you already wrote the line for c1 , so mirror it for c2 .

Your Turn 2: finish the update step

Now move each centre to the mean of its points. The c1 line is done — write the matching one for c2 .

3 DBSCAN: Clusters of Any Shape + Outliers

K-Means has two weaknesses: you must pick K, and it only finds round blobs. DBSCAN (Density-Based Spatial Clustering) fixes both. It grows clusters wherever points are densely packed, finds any shape , decides the number of clusters itself, and labels lonely points as noise (id -1 ) — free anomaly detection.

Two knobs control it: eps (how close counts as "neighbour") and min_samples (how many neighbours make a dense core).

4 Hierarchical Clustering: a Tree of Groups

Hierarchical (agglomerative) clustering starts with every point as its own tiny cluster, then repeatedly merges the two closest clusters until one remains. The record of merges is a tree called a dendrogram . The trick: you don't choose K up front — you cut the tree at any height afterwards. Cut high for few big clusters, cut low for many small ones.

Each row of the merge table shows which two clusters joined and at what distance. The big jump in distance on the last merges is your cue for where to cut — that's the dendrogram telling you the natural number of groups.

5 Choosing K: Elbow + Silhouette

K-Means won't pick K for you, so try a range and compare two numbers. The elbow method plots inertia against K — it always falls, but you want the "elbow" where it stops falling fast. The silhouette score (from -1 to 1) measures how tight and separated the clusters are; pick the K with the highest score. When both agree, you're confident.

🧭 Beyond Clustering: Association & Anomaly Detection

Clustering is the headline act, but two other unsupervised jobs power real products — and neither needs labels:

Find items that show up together in baskets — "customers who buy nappies also buy beer." The Apriori algorithm mines these rules from transaction logs to drive recommendations and shelf layout.

Flag the rare, weird points — fraud, failing sensors, intrusions. Isolation Forest scores how easily each point is "isolated"; DBSCAN's -1 noise label is a simpler version of the same idea.

You'll meet dimensionality reduction (PCA, t-SNE) — squeezing many features into a few for plotting and speed — in the very next lesson.

6 Common Errors (And How to Fix Them)

These four mistakes trip up nearly every beginner. Spotting them early saves hours.

Pick K=2 when the data really has 5 groups and K-Means happily merges distinct groups into mush. There's no error message — just bad clusters.

✅ Fix: never hard-code K blindly. Run the elbow + silhouette loop from Section 5 first, or switch to DBSCAN/hierarchical, which decide K for you.

Cluster on [income (0–100000), age (18–90)] and income's huge range drowns out age entirely — distance becomes "income distance".

✅ Fix: always scale features before any distance-based clustering.

❌ Assuming spherical clusters — K-Means on the wrong shape

K-Means assumes round, similarly-sized blobs. On crescents, rings, or stretched bands it slices straight through the real structure and the clusters look nonsensical.

✅ Fix: if your clusters aren't roughly spherical, use DBSCAN (density, any shape) or a Gaussian Mixture Model instead.

❌ No random init control — unstable, irreproducible results

K-Means starts from random centroids, so a single run can land in a bad local optimum and give different clusters every time.

✅ Fix: set n_init=10 and a fixed random_state .

📋 Quick Reference

Algorithm / tool

Use it when

Watch out for

KMeans(n_clusters=K)

Round blobs, you can pick K, big data

Must choose K; scale features; set n_init

DBSCAN(eps, min_samples)

Any shape, unknown K, want outliers

Sensitive to eps / min_samples

linkage + fcluster

Want a dendrogram, cut K afterwards

Slow on large data (~O(n²–n³))

silhouette_score

Compare K values (higher = better)

Slower than inertia on big data

.inertia_ (elbow)

Spot the K where spread flattens

Always falls — read the elbow, not the min

StandardScaler

Before any distance-based clustering

Forget it and one feature dominates

Frequently Asked Questions

Mini-Challenge: segment your customers

No blanks this time — just a comment outline. Build the array, fit KMeans , and print the labels. The low spenders should share one group, the high spenders the other.

🎉 Lesson Complete!

You can now find hidden structure in unlabelled data. You ran a K-Means step by hand (assign + update), clustered in two lines with scikit-learn, chose K with the elbow and silhouette methods, reached for DBSCAN and hierarchical clustering when blobs aren't enough, and saw where association rules and anomaly detection fit.

🚀 Up next: Dimensionality Reduction — squeeze high-dimensional data down to two or three features so you can visualise it and train faster, using PCA and t-SNE.

Practice quiz

What distinguishes unsupervised learning from supervised learning?

  • Unsupervised learning always needs more labels
  • Unsupervised learning only works on images
  • Unsupervised learning uses no labels and discovers structure on its own
  • Unsupervised learning requires a GPU

Answer: Unsupervised learning uses no labels and discovers structure on its own. Supervised learning trains on labelled examples; unsupervised learning gets no labels and finds structure (groups, anomalies) in the raw features.

What two steps does K-Means repeat in a loop?

  • Assign each point to its nearest centroid, then update each centroid to the mean of its points
  • Forward pass and backward pass
  • Difference and integrate
  • Encode and decode

Answer: Assign each point to its nearest centroid, then update each centroid to the mean of its points. K-Means alternates Assign (each point joins its nearest centroid) and Update (move each centroid to the mean of its points) until centroids stop moving.

What is a centroid in K-Means?

  • The largest point in the dataset
  • An outlier
  • The number of clusters
  • The centre point of a cluster

Answer: The centre point of a cluster. A centroid is the centre of a cluster; K-Means moves each centroid to the mean of the points assigned to it.

What is the 'elbow method' used for?

  • Choosing the learning rate
  • Choosing K by finding where inertia stops falling sharply as K increases
  • Removing outliers
  • Scaling features

Answer: Choosing K by finding where inertia stops falling sharply as K increases. Inertia always falls as K rises; the 'elbow' is where it stops falling sharply — that K captures the structure without over-splitting.

What does the silhouette score measure?

  • How tight and well-separated the clusters are (higher is better, range -1 to 1)
  • The training time
  • The number of features
  • The total memory used

Answer: How tight and well-separated the clusters are (higher is better, range -1 to 1). The silhouette score ranges from -1 to 1; higher means clusters are tight and well-separated. You pick the K with the highest score.

When should you use DBSCAN instead of K-Means?

  • When you have exactly two round clusters
  • When all features are already scaled and round
  • When clusters are non-spherical, K is unknown, or you need outliers flagged
  • When you have labelled data

Answer: When clusters are non-spherical, K is unknown, or you need outliers flagged. DBSCAN groups by density: it finds arbitrary shapes, decides the number of clusters itself, and labels low-density points as noise (-1).

What do DBSCAN's two main parameters, eps and min_samples, control?

  • The learning rate and batch size
  • The neighbourhood radius and how many points form a dense core
  • The number of clusters and the number of iterations
  • The feature scaling and the random seed

Answer: The neighbourhood radius and how many points form a dense core. eps sets how close counts as a neighbour; min_samples sets how many neighbours are needed to form a dense core cluster.

In hierarchical (agglomerative) clustering, how is the number of clusters chosen?

  • You must set it before training
  • It is always two
  • By the silhouette score only
  • By cutting the dendrogram (tree of merges) at a chosen height afterwards

Answer: By cutting the dendrogram (tree of merges) at a chosen height afterwards. Agglomerative clustering merges closest clusters into a dendrogram; you cut the tree at any height afterwards — high for few clusters, low for many.

Why must features be scaled before distance-based clustering?

  • To speed up training only
  • Otherwise the feature with the largest numeric range dominates the distance and others are ignored
  • To convert them to integers
  • Scaling is never needed for clustering

Answer: Otherwise the feature with the largest numeric range dominates the distance and others are ignored. Distance is dominated by whichever feature has the biggest range, so income (0-100000) would drown out age (18-90). Standardise so each contributes fairly.

Which is an example of an unsupervised task beyond clustering?

  • Linear regression
  • Image classification with labels
  • Anomaly detection (e.g. Isolation Forest flagging rare points)
  • Supervised fine-tuning

Answer: Anomaly detection (e.g. Isolation Forest flagging rare points). Anomaly detection and association rule mining (Apriori) are unsupervised tasks; Isolation Forest scores how 'odd' each point is, without any labels.

Continue this course

Related lessons