Find clusters of any shape. Watch DBSCAN grow clusters from dense points and flag noise, pick ε from the k-distance plot, build a dendrogram merge by merge, cut it, and compare linkages.
1K-Means can only cut straight
K-Means gives every point to its nearest centre, so its clusters are always round-ish blobs split by straight lines. Two interlocking moons get cut in half, the wrong way.
2Core, border and noise
DBSCAN draws a circle of radius ε round every point. With at least minPts points inside (4 here, counting itself), it is a core point. Core points that reach each other form a cluster; points near a core point join as border points; the rest are noise.
3Merge, merge, merge, then cut
Hierarchical clustering starts with every point alone and keeps merging the two closest clusters. The dendrogram records each merge at its height. Cut it with a horizontal line: the branches it crosses are your clusters.
Before you start
Is it right for your data?
Finds groups in unlabelled data: DBSCAN by density (with noise), hierarchical as a tree.
- TablesGreat fit
spreadsheets, databases, CSVs
Numeric data with few dimensions, and map coordinates.
- TextWith work
emails, reviews, documents
On embeddings, usually after reducing them to a few dimensions.
- ImagesWith work
photos, scans, video frames
On embeddings, not pixels.
- Audio & time seriesWith work
sound, sensors, prices over time
On summary features, or with a sequence-aware distance such as dynamic time warping; plain distances don't line up shifted sequences.
- How much data
- DBSCAN: up to millions of points in low dimensions with a spatial index. Hierarchical: up to about 10,000 points.
- Preparation
- Scale features. Choose ε and minPts (DBSCAN) or a linkage and a cut (hierarchical).
- What it can learn
- DBSCAN: clusters of any shape, plus noise. Hierarchical: nested groups at every scale.
- Explainable?
- Partly: the dendrogram shows how groups join; DBSCAN marks the outliers.
Use it when
You don't know how many groups there are, the shapes are odd, or you want outliers flagged: GPS stops, fraud rings.
Avoid it when
The data has many dimensions, the clusters have very different densities, or (for hierarchical) it's large.
If not this, try: K-Means Clustering (round groups, big data, k known) · Principal Component Analysis (reduce dimensions before clustering)
Start here
Where K-Means breaks
K-Means gives every point to its nearest centre, so it can only make round clusters, and it needs to be told how many. Real groups are often curved, nested or full of stray points. Pick a dataset and compare it with DBSCAN, which looks for dense regions instead.
K-Means, k = 2
- Grouped right
- 66%
- Clusters
- 2
- Noise
- –
DBSCAN, ε = 0.6, minPts = 4
- Grouped right
- 98%
- Clusters
- 2
- Noise
- 8
Two crescents that hook into each other. K-Means draws a straight border and cuts both moons in half; DBSCAN walks along each curve because the points along it are dense, and leaves the scattered points out as noise.
“Grouped right” matches each found cluster to the true shape it overlaps most, and counts a scattered point as right only when it's called noise. K-Means can't call anything noise, so it loses those points by design.
How it works
DBSCAN, one point at a time
Go through the points in order. A point with at least minPts points inside its ε-circle (itself included) is a core point and starts a cluster. The cluster then spreads: every neighbour joins, and every neighbour that is core too passes the spread on. Points that are near a core point but not core themselves join as border points and stop the spread. The rest are noise. Click the plot to add points.
- Clusters
- 0
- Core
- 0
- Border
- 0
- Noise
- 0
Press Start to run DBSCAN through the points in order.
The two settings
Choosing ε and minPts
DBSCAN has no k, but ε matters a lot. Too small and everything is noise; too big and the clusters run together. The classic trick is the k-distance plot: for every point, measure the distance to its minPts-th nearest neighbour and sort them. Points inside clusters have small distances; noise has big ones. Put ε near the elbow where the curve shoots up. Click the curve to set ε.
Shading: the ε-circles of the core points. A cluster is a connected patch of them.
Every point's distance to its 4th nearest point (itself counted first), sorted from small to large.
- Clusters
- 2
- Noise
- 8
- Grouped right
- 98%
The elbow is a starting point, not an answer: nudge ε up or down and watch how stable the clusters are. A rule of thumb for minPts is about twice the number of features, and at least 3.
A different idea
Hierarchical clustering: grow a tree, then cut it
Agglomerative clustering starts with every point as its own cluster and merges the two closest clusters, again and again, until one is left. The dendrogram below records every merge at its height (the distance at that merge). Press Start to watch it grow, then move the cut to choose how many clusters you want, without running anything again.
- Clusters
- 3
- Merges
- 9 of 11
The full tree for single linkage is drawn below. Press Start to build it one merge at a time.
Look for the tallest stretch of the tree with no merges in it and cut there: that's the number of clusters the data suggests most strongly (4 for single linkage). Click the dendrogram to place the cut.
One choice, four answers
Linkage matters
Once a cluster has several points, “distance between clusters” needs a rule. Each panel cuts its tree at the true number of groups. Single linkage follows chains, so it traces curves perfectly, until a few stray points build a bridge. Complete, average and Ward keep clusters compact and round, like K-Means.
Single
100%Complete
78%Average
78%Ward
84%Without noise, single linkage is perfect: each moon or ring is a chain of close neighbours. Ward, complete and average want compact groups, so they cut across the curves, much as K-Means does. Now turn the noise on.
Know their limits
The catch, and which to use
One density for everything
DBSCAN's single ε can't fit a tight cluster and a sparse one together. HDBSCAN and OPTICS handle uneven density by trying every ε.
Scale first
ε is one radius in every direction, and linkages use distances too, so a feature in big units dominates. Standardise first. Scaling, on the KNN page →
High dimensions
In hundreds of dimensions all distances look alike, so “dense” loses its meaning. Reduce the dimensions first. PCA →
Memory for the tree
Hierarchical clustering keeps every pairwise distance: 10,000 points is 50 million pairs. Beyond that, sample the data or use DBSCAN.
Which to use: K-Means when you expect round groups and know roughly how many; DBSCAN (or HDBSCAN) for odd shapes, outliers, and an unknown number of clusters; hierarchical clustering for a few thousand points when the nested structure is the point, or when you want to choose k from the dendrogram.
No magic, just arithmetic
The maths, step by step
Six points: A (1, 1), B (1.5, 1), C (1, 1.6) close together; D (4, 4) and E (4.5, 4) as a pair; F (7, 1) on its own. First DBSCAN with ε = 0.8 and minPts = 3, then the start of the hierarchical tree.
- 1
Distances
Everything below uses the ordinary straight-line (Euclidean) distance between two points.d(A, B) = √((1.5 − 1)² + (1 − 1)²) = 0.5000
d(A, C) = √((1 − 1)² + (1.6 − 1)²) = 0.6000
d(B, C) = √((1.5 − 1)² + (1 − 1.6)²) = 0.7810
d(D, E) = √((4.5 − 4)² + (4 − 4)²) = 0.5000
Every other pair is more than 3 apart.
- 2
Count the neighbours within ε
A point is core when at least 3 points, itself included, lie within ε = 0.8.A: A, B, C → 3 core
B: A, B, C → 3 core
C: A, B, C → 3 core
D: D, E → 2 not core
E: D, E → 2 not core
F: F → 1 not core
- 3
Join the core points, then label the rest
Core points within ε of each other share a cluster. A non-core point within ε of a core point is a border point; anything else is noise.Cluster 1: A, B, C (all core)
Noise: D, E, F
D and E are close to each other, but two points never reach minPts = 3, and neither is near a core point. So DBSCAN calls them noise: a pair isn't dense enough to count as a cluster.
- 4
Hierarchical: merge the closest pair
Now forget ε. Start with six clusters of one point each and merge the closest two. A–B and D–E tie at 0.5; take A–B first.merge 1: A + B = height = 0.5000
merge 2: D + E = height = 0.5000
- 5
How far is C from the cluster {A, B}?
Now one side is a cluster, so you need a rule for cluster-to-cluster distance. That rule is the linkage, and each gives a different answer.single (closest pair) = min(d(A, C), d(B, C)) = min(0.6, 0.781) = 0.6000
complete (farthest pair) = max(0.6, 0.781) = 0.7810
average (all pairs) = (0.6 + 0.781) ÷ 2 = 0.6905
Ward = √(2·2·1 ÷ 3) × |(1.25, 1) − (1, 1.6)| = 1.1547 × 0.65 = 0.7506
Ward measures how much the total squared distance to the cluster centres would grow, so it prefers merging small, tight groups.
- 6
Cut the finished tree
Keep merging until one cluster is left: single linkage's heights are 0.5, 0.5, 0.6, 3.842, 3.905. A cut at height 2 keeps every merge below it.clusters left at height 2 = 6 points − 3 merges below the cut = 3
Clusters: {A, B, C}, {D, E}, {F}
Unlike DBSCAN, hierarchical clustering never calls anything noise: F simply stays a cluster of one until the last merges.
Keep going
Resources
Hand-picked places to go deeper. They're all free to read or watch, and they open in a new tab.
How it works: DBSCAN & Hierarchical Clustering
Step by step
- A cluster (each cluster gets its own colour)
- Noise, or a point not yet visited
- Queued to be expanded
- The dendrogram's cut line
Press Start or Step: the step the animation is on lights up below.
- 1
Start: nothing grouped
DBSCAN starts with every point unvisited. Hierarchical clustering starts with every point as a cluster of one.
label = [UNVISITED] * n | clusters = [{p} for p in X] - 2
Find a core point
Take the next unvisited point and count the points within ε. With at least minPts, it is core: start a new cluster and queue its neighbours.
if |N_ε(p)| ≥ minPts: c += 1; label[p] = c; queue = N_ε(p) - 3
Grow the cluster
Pop a queued point and give it the cluster. If it is core too, queue its unclaimed neighbours, so the cluster spreads through every dense path.
q = queue.pop(); label[q] = c; if |N_ε(q)| ≥ minPts: queue += N_ε(q) - 4
Mark noise
A point with too few neighbours that no cluster has claimed is noise, for now. A later cluster can still claim it as a border point.
else: label[p] = NOISE - 5
Clusters and noise
Every point is a core or border point of some cluster, or noise. The number of clusters came out of the data.
return label - 6
Merge the closest pair
Find the two clusters with the smallest linkage distance and merge them; record the merge and its height for the dendrogram.
A, B = argmin d(A, B); merge(A, B); record(A, B, d) - 7
Cut the dendrogram
Once one cluster is left the tree is complete. A horizontal cut at height h keeps the merges below h; the branches it crosses are the clusters.
labels = cut(tree, h) # or keep k clusters
K-Means assumes round clusters and needs k up front. Two other families avoid both. DBSCAN (density-based spatial clustering of applications with noise) defines a cluster as a dense region: a point with at least minPts points within distance ε (itself included) is a core point; core points within ε of each other share a cluster; a non-core point within ε of a core point joins as a border point; everything else is noise. It finds clusters of any shape and decides how many there are.
Hierarchical (agglomerative) clustering starts with every point as its own cluster and repeatedly merges the two closest clusters until one is left. The record of merges is a tree, the dendrogram, with each merge drawn at its distance. Cutting the tree at a height (or at k clusters) gives a flat clustering, so one run serves every k.
The linkage decides what “closest clusters” means: single (closest pair of points) follows chains and curves but is fooled by noise bridges; complete (farthest pair) and average make compact groups; Ward merges the pair that adds least to the within-cluster squared error, giving K-Means-like round clusters.
Where you'll meet it
- DBSCAN: clusters of odd shapes, data with outliers to flag (fraud, sensor glitches, GPS stop detection), and when you don't know how many clusters there are.
- Hierarchical: small to medium data where the nested structure itself matters (taxonomies, gene expression, customer segments at several levels), or when you want to choose k by looking at the dendrogram.
- Interview favourites: “what do ε and minPts do?”, “core vs border vs noise?”, “how do you pick ε?” (the k-distance elbow), “where does DBSCAN fail?” (uneven density), “single vs complete vs Ward linkage?”, “how do you read a dendrogram?”.
DBSCAN does one ε-range query per point; a KD-tree or ball tree makes each about log n in low dimensions. Agglomerative clustering keeps the n × n distance matrix and does n − 1 merges, each scanning for the closest pair: O(n³) naively, O(n²) with SLINK (single) or a priority queue per cluster.