DBSCAN & Hierarchical Clustering

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.

K-Means' split

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.

core: 7 inside εborder: 3noise: 1

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.

cut → 2 clustersheight = merge distance

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.

Dataset

K-Means, k = 2

02468100246810Point 1 (6.23, 4.31), cluster 1Point 2 (5.99, 4.67), cluster 1Point 3 (6.13, 4.9), cluster 1Point 4 (5.79, 4.89), cluster 1Point 5 (5.92, 5.31), cluster 1Point 6 (6.13, 5.38), cluster 1Point 7 (5.93, 5.47), cluster 1Point 8 (5.95, 5.42), cluster 1Point 9 (6.08, 5.99), cluster 1Point 10 (6.05, 5.8), cluster 1Point 11 (5.79, 6.02), cluster 1Point 12 (5.3, 6.34), cluster 2Point 13 (5.22, 6.13), cluster 2Point 14 (5.28, 6.15), cluster 2Point 15 (4.98, 6.61), cluster 2Point 16 (5.19, 6.64), cluster 2Point 17 (5.03, 6.49), cluster 2Point 18 (4.85, 6.92), cluster 2Point 19 (4.72, 6.21), cluster 2Point 20 (4.67, 6.63), cluster 2Point 21 (3.83, 7.11), cluster 2Point 22 (4.08, 6.66), cluster 2Point 23 (3.49, 6.81), cluster 2Point 24 (3.66, 7.03), cluster 2Point 25 (3.75, 6.55), cluster 2Point 26 (3.62, 6.91), cluster 2Point 27 (3.24, 6.84), cluster 2Point 28 (3.09, 6.44), cluster 2Point 29 (2.73, 6.65), cluster 2Point 30 (2.64, 6.81), cluster 2Point 31 (2.5, 5.94), cluster 2Point 32 (2.26, 6.56), cluster 2Point 33 (2.14, 6.01), cluster 2Point 34 (2.13, 5.91), cluster 2Point 35 (2.14, 6.13), cluster 2Point 36 (1.98, 6.41), cluster 2Point 37 (1.71, 5.72), cluster 2Point 38 (1.6, 5.51), cluster 2Point 39 (1.29, 5.15), cluster 2Point 40 (1.51, 5.34), cluster 2Point 41 (1.52, 5.32), cluster 2Point 42 (1.16, 5.15), cluster 2Point 43 (1.34, 4.93), cluster 2Point 44 (1.61, 4.42), cluster 2Point 45 (1.22, 4.61), cluster 2Point 46 (4.06, 5.13), cluster 2Point 47 (3.74, 4.91), cluster 2Point 48 (3.99, 4.95), cluster 2Point 49 (4.04, 4.96), cluster 2Point 50 (3.88, 5.17), cluster 2Point 51 (3.96, 4.69), cluster 2Point 52 (4.14, 4.91), cluster 2Point 53 (4.26, 4.24), cluster 2Point 54 (4.17, 4.02), cluster 2Point 55 (4.08, 4.09), cluster 2Point 56 (4.04, 3.98), cluster 2Point 57 (4.32, 4.07), cluster 2Point 58 (4.94, 3.74), cluster 1Point 59 (4.6, 3.93), cluster 2Point 60 (5.13, 3.4), cluster 1Point 61 (4.87, 3.3), cluster 1Point 62 (5.65, 3.79), cluster 1Point 63 (5.38, 3.43), cluster 1Point 64 (5.48, 3.27), cluster 1Point 65 (5.63, 3.19), cluster 1Point 66 (6.35, 3.16), cluster 1Point 67 (5.91, 3.05), cluster 1Point 68 (6.11, 2.99), cluster 1Point 69 (6.38, 3.15), cluster 1Point 70 (6.9, 3.2), cluster 1Point 71 (6.61, 3.02), cluster 1Point 72 (7.01, 3.39), cluster 1Point 73 (7.01, 3.56), cluster 1Point 74 (6.96, 3.59), cluster 1Point 75 (7.48, 3.52), cluster 1Point 76 (7.88, 3.61), cluster 1Point 77 (7.16, 3.59), cluster 1Point 78 (7.62, 3.7), cluster 1Point 79 (8.05, 3.8), cluster 1Point 80 (8.16, 3.93), cluster 1Point 81 (8.07, 4.17), cluster 1Point 82 (8.44, 4.17), cluster 1Point 83 (8.02, 4.28), cluster 1Point 84 (8.22, 4.41), cluster 1Point 85 (8.28, 4.58), cluster 1Point 86 (8.43, 4.85), cluster 1Point 87 (8.3, 5.1), cluster 1Point 88 (8.47, 5.05), cluster 1Point 89 (8.83, 5.55), cluster 1Point 90 (8.28, 5.39), cluster 1Point 91 (9.15, 3.69), cluster 1Point 92 (6.81, 5.04), cluster 1Point 93 (9.36, 3.49), cluster 1Point 94 (1.89, 8.61), cluster 2Point 95 (1.75, 3.49), cluster 2Point 96 (0.81, 5.61), cluster 2Point 97 (8.53, 5.04), cluster 1Point 98 (0.9, 3.12), cluster 2Point 99 (0.52, 6.92), cluster 2Point 100 (2.13, 3.39), cluster 2
Grouped right
66%
Clusters
2
Noise
–

DBSCAN, ε = 0.6, minPts = 4

02468100246810Point 1 (6.23, 4.31): border, cluster 1Point 2 (5.99, 4.67): core, cluster 1Point 3 (6.13, 4.9): core, cluster 1Point 4 (5.79, 4.89): core, cluster 1Point 5 (5.92, 5.31): core, cluster 1Point 6 (6.13, 5.38): core, cluster 1Point 7 (5.93, 5.47): core, cluster 1Point 8 (5.95, 5.42): core, cluster 1Point 9 (6.08, 5.99): core, cluster 1Point 10 (6.05, 5.8): core, cluster 1Point 11 (5.79, 6.02): core, cluster 1Point 12 (5.3, 6.34): core, cluster 1Point 13 (5.22, 6.13): core, cluster 1Point 14 (5.28, 6.15): core, cluster 1Point 15 (4.98, 6.61): core, cluster 1Point 16 (5.19, 6.64): core, cluster 1Point 17 (5.03, 6.49): core, cluster 1Point 18 (4.85, 6.92): core, cluster 1Point 19 (4.72, 6.21): core, cluster 1Point 20 (4.67, 6.63): core, cluster 1Point 21 (3.83, 7.11): core, cluster 1Point 22 (4.08, 6.66): core, cluster 1Point 23 (3.49, 6.81): core, cluster 1Point 24 (3.66, 7.03): core, cluster 1Point 25 (3.75, 6.55): core, cluster 1Point 26 (3.62, 6.91): core, cluster 1Point 27 (3.24, 6.84): core, cluster 1Point 28 (3.09, 6.44): core, cluster 1Point 29 (2.73, 6.65): core, cluster 1Point 30 (2.64, 6.81): core, cluster 1Point 31 (2.5, 5.94): core, cluster 1Point 32 (2.26, 6.56): core, cluster 1Point 33 (2.14, 6.01): core, cluster 1Point 34 (2.13, 5.91): core, cluster 1Point 35 (2.14, 6.13): core, cluster 1Point 36 (1.98, 6.41): core, cluster 1Point 37 (1.71, 5.72): core, cluster 1Point 38 (1.6, 5.51): core, cluster 1Point 39 (1.29, 5.15): core, cluster 1Point 40 (1.51, 5.34): core, cluster 1Point 41 (1.52, 5.32): core, cluster 1Point 42 (1.16, 5.15): core, cluster 1Point 43 (1.34, 4.93): core, cluster 1Point 44 (1.61, 4.42): border, cluster 1Point 45 (1.22, 4.61): core, cluster 1Point 46 (4.06, 5.13): core, cluster 2Point 47 (3.74, 4.91): core, cluster 2Point 48 (3.99, 4.95): core, cluster 2Point 49 (4.04, 4.96): core, cluster 2Point 50 (3.88, 5.17): core, cluster 2Point 51 (3.96, 4.69): core, cluster 2Point 52 (4.14, 4.91): core, cluster 2Point 53 (4.26, 4.24): core, cluster 2Point 54 (4.17, 4.02): core, cluster 2Point 55 (4.08, 4.09): core, cluster 2Point 56 (4.04, 3.98): core, cluster 2Point 57 (4.32, 4.07): core, cluster 2Point 58 (4.94, 3.74): core, cluster 2Point 59 (4.6, 3.93): core, cluster 2Point 60 (5.13, 3.4): core, cluster 2Point 61 (4.87, 3.3): core, cluster 2Point 62 (5.65, 3.79): border, cluster 2Point 63 (5.38, 3.43): core, cluster 2Point 64 (5.48, 3.27): core, cluster 2Point 65 (5.63, 3.19): core, cluster 2Point 66 (6.35, 3.16): core, cluster 2Point 67 (5.91, 3.05): core, cluster 2Point 68 (6.11, 2.99): core, cluster 2Point 69 (6.38, 3.15): core, cluster 2Point 70 (6.9, 3.2): core, cluster 2Point 71 (6.61, 3.02): core, cluster 2Point 72 (7.01, 3.39): core, cluster 2Point 73 (7.01, 3.56): core, cluster 2Point 74 (6.96, 3.59): core, cluster 2Point 75 (7.48, 3.52): core, cluster 2Point 76 (7.88, 3.61): core, cluster 2Point 77 (7.16, 3.59): core, cluster 2Point 78 (7.62, 3.7): core, cluster 2Point 79 (8.05, 3.8): core, cluster 2Point 80 (8.16, 3.93): core, cluster 2Point 81 (8.07, 4.17): core, cluster 2Point 82 (8.44, 4.17): core, cluster 2Point 83 (8.02, 4.28): core, cluster 2Point 84 (8.22, 4.41): core, cluster 2Point 85 (8.28, 4.58): core, cluster 2Point 86 (8.43, 4.85): core, cluster 2Point 87 (8.3, 5.1): core, cluster 2Point 88 (8.47, 5.05): core, cluster 2Point 89 (8.83, 5.55): border, cluster 2Point 90 (8.28, 5.39): core, cluster 2Point 91 (9.15, 3.69): noisePoint 92 (6.81, 5.04): noisePoint 93 (9.36, 3.49): noisePoint 94 (1.89, 8.61): noisePoint 95 (1.75, 3.49): noisePoint 96 (0.81, 5.61): border, cluster 1Point 97 (8.53, 5.04): core, cluster 2Point 98 (0.9, 3.12): noisePoint 99 (0.52, 6.92): noisePoint 100 (2.13, 3.39): noise
Grouped right
98%
Clusters
2
Noise
8
a cluster (one colour each)DBSCAN border point×noise: in no cluster

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.

02468100246810Point 1 (5.99, 4.45)Point 2 (6.32, 4.68)Point 3 (6.08, 5.17)Point 4 (5.76, 5.63)Point 5 (5.59, 6.19)Point 6 (5.52, 5.85)Point 7 (5.19, 6.61)Point 8 (4.71, 6.44)Point 9 (4.56, 6.4)Point 10 (4.49, 6.75)Point 11 (4.09, 6.92)Point 12 (3.83, 7)Point 13 (3.27, 6.79)Point 14 (3, 6.81)Point 15 (2.7, 6.53)Point 16 (2.36, 6.48)Point 17 (2.22, 6.08)Point 18 (2.03, 5.84)Point 19 (1.63, 5.47)Point 20 (1.41, 5.24)Point 21 (1.56, 4.71)Point 22 (1.34, 4.29)Point 23 (3.78, 5.49)Point 24 (4.01, 5.33)Point 25 (4.16, 5.06)Point 26 (4.39, 4.26)Point 27 (4.12, 4.22)Point 28 (4.25, 3.75)Point 29 (5.2, 3.41)Point 30 (4.79, 3.18)Point 31 (5.51, 3.49)Point 32 (5.88, 3.26)Point 33 (6.29, 2.94)Point 34 (6.22, 2.96)Point 35 (6.66, 3.31)Point 36 (7.23, 3.44)Point 37 (6.94, 3.09)Point 38 (7.56, 3.8)Point 39 (7.81, 3.73)Point 40 (8.15, 3.88)Point 41 (8, 4.56)Point 42 (8.35, 4.8)Point 43 (8.63, 5.05)Point 44 (8.51, 5.4)Point 45 (0.76, 9.22)Point 46 (7.58, 3.17)Point 47 (2.93, 0.42)Point 48 (3.04, 5.74)Point 49 (1.24, 1.77)
core pointborder point×noisequeued to expandε-circle of the point being checked
0.85
4
Clusters
0
Core
0
Border
0
Noise
0
Step 0 / 53
500 ms

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 ε.

Dataset
02468100246810Point 1 (6.23, 4.31): border, cluster 1Point 2 (5.99, 4.67): core, cluster 1Point 3 (6.13, 4.9): core, cluster 1Point 4 (5.79, 4.89): core, cluster 1Point 5 (5.92, 5.31): core, cluster 1Point 6 (6.13, 5.38): core, cluster 1Point 7 (5.93, 5.47): core, cluster 1Point 8 (5.95, 5.42): core, cluster 1Point 9 (6.08, 5.99): core, cluster 1Point 10 (6.05, 5.8): core, cluster 1Point 11 (5.79, 6.02): core, cluster 1Point 12 (5.3, 6.34): core, cluster 1Point 13 (5.22, 6.13): core, cluster 1Point 14 (5.28, 6.15): core, cluster 1Point 15 (4.98, 6.61): core, cluster 1Point 16 (5.19, 6.64): core, cluster 1Point 17 (5.03, 6.49): core, cluster 1Point 18 (4.85, 6.92): core, cluster 1Point 19 (4.72, 6.21): core, cluster 1Point 20 (4.67, 6.63): core, cluster 1Point 21 (3.83, 7.11): core, cluster 1Point 22 (4.08, 6.66): core, cluster 1Point 23 (3.49, 6.81): core, cluster 1Point 24 (3.66, 7.03): core, cluster 1Point 25 (3.75, 6.55): core, cluster 1Point 26 (3.62, 6.91): core, cluster 1Point 27 (3.24, 6.84): core, cluster 1Point 28 (3.09, 6.44): core, cluster 1Point 29 (2.73, 6.65): core, cluster 1Point 30 (2.64, 6.81): core, cluster 1Point 31 (2.5, 5.94): core, cluster 1Point 32 (2.26, 6.56): core, cluster 1Point 33 (2.14, 6.01): core, cluster 1Point 34 (2.13, 5.91): core, cluster 1Point 35 (2.14, 6.13): core, cluster 1Point 36 (1.98, 6.41): core, cluster 1Point 37 (1.71, 5.72): core, cluster 1Point 38 (1.6, 5.51): core, cluster 1Point 39 (1.29, 5.15): core, cluster 1Point 40 (1.51, 5.34): core, cluster 1Point 41 (1.52, 5.32): core, cluster 1Point 42 (1.16, 5.15): core, cluster 1Point 43 (1.34, 4.93): core, cluster 1Point 44 (1.61, 4.42): border, cluster 1Point 45 (1.22, 4.61): core, cluster 1Point 46 (4.06, 5.13): core, cluster 2Point 47 (3.74, 4.91): core, cluster 2Point 48 (3.99, 4.95): core, cluster 2Point 49 (4.04, 4.96): core, cluster 2Point 50 (3.88, 5.17): core, cluster 2Point 51 (3.96, 4.69): core, cluster 2Point 52 (4.14, 4.91): core, cluster 2Point 53 (4.26, 4.24): core, cluster 2Point 54 (4.17, 4.02): core, cluster 2Point 55 (4.08, 4.09): core, cluster 2Point 56 (4.04, 3.98): core, cluster 2Point 57 (4.32, 4.07): core, cluster 2Point 58 (4.94, 3.74): core, cluster 2Point 59 (4.6, 3.93): core, cluster 2Point 60 (5.13, 3.4): core, cluster 2Point 61 (4.87, 3.3): core, cluster 2Point 62 (5.65, 3.79): border, cluster 2Point 63 (5.38, 3.43): core, cluster 2Point 64 (5.48, 3.27): core, cluster 2Point 65 (5.63, 3.19): core, cluster 2Point 66 (6.35, 3.16): core, cluster 2Point 67 (5.91, 3.05): core, cluster 2Point 68 (6.11, 2.99): core, cluster 2Point 69 (6.38, 3.15): core, cluster 2Point 70 (6.9, 3.2): core, cluster 2Point 71 (6.61, 3.02): core, cluster 2Point 72 (7.01, 3.39): core, cluster 2Point 73 (7.01, 3.56): core, cluster 2Point 74 (6.96, 3.59): core, cluster 2Point 75 (7.48, 3.52): core, cluster 2Point 76 (7.88, 3.61): core, cluster 2Point 77 (7.16, 3.59): core, cluster 2Point 78 (7.62, 3.7): core, cluster 2Point 79 (8.05, 3.8): core, cluster 2Point 80 (8.16, 3.93): core, cluster 2Point 81 (8.07, 4.17): core, cluster 2Point 82 (8.44, 4.17): core, cluster 2Point 83 (8.02, 4.28): core, cluster 2Point 84 (8.22, 4.41): core, cluster 2Point 85 (8.28, 4.58): core, cluster 2Point 86 (8.43, 4.85): core, cluster 2Point 87 (8.3, 5.1): core, cluster 2Point 88 (8.47, 5.05): core, cluster 2Point 89 (8.83, 5.55): border, cluster 2Point 90 (8.28, 5.39): core, cluster 2Point 91 (9.15, 3.69): noisePoint 92 (6.81, 5.04): noisePoint 93 (9.36, 3.49): noisePoint 94 (1.89, 8.61): noisePoint 95 (1.75, 3.49): noisePoint 96 (0.81, 5.61): border, cluster 1Point 97 (8.53, 5.04): core, cluster 2Point 98 (0.9, 3.12): noisePoint 99 (0.52, 6.92): noisePoint 100 (2.13, 3.39): noise

Shading: the ε-circles of the core points. A cluster is a connected patch of them.

0509900.511.522.5ε = 0.60above the line: not core

Every point's distance to its 4th nearest point (itself counted first), sorted from small to large.

0.60
4
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.

024681002468101Point 1 (1.6, 2.2)2Point 2 (2.4, 1.6)3Point 3 (2.1, 3.1)4Point 4 (3.1, 2.4)5Point 5 (6.8, 2)6Point 6 (7.6, 2.8)7Point 7 (8.4, 1.8)8Point 8 (4.6, 6.8)9Point 9 (5.4, 7.6)10Point 10 (4.4, 8)11Point 11 (6, 6.6)12Point 12 (5, 4.6)
a point still on its ownthe closest pair that set the merge distance
Linkage
Step 0 / 12
900 ms
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.

00.511.522.53756431212118910cut at 2.57
3

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

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.

Dataset
Scattered noise

Single

100%
02468100246810Point 1 (6.23, 4.31)Point 2 (5.99, 4.67)Point 3 (6.13, 4.9)Point 4 (5.79, 4.89)Point 5 (5.92, 5.31)Point 6 (6.13, 5.38)Point 7 (5.93, 5.47)Point 8 (5.95, 5.42)Point 9 (6.08, 5.99)Point 10 (6.05, 5.8)Point 11 (5.79, 6.02)Point 12 (5.3, 6.34)Point 13 (5.22, 6.13)Point 14 (5.28, 6.15)Point 15 (4.98, 6.61)Point 16 (5.19, 6.64)Point 17 (5.03, 6.49)Point 18 (4.85, 6.92)Point 19 (4.72, 6.21)Point 20 (4.67, 6.63)Point 21 (3.83, 7.11)Point 22 (4.08, 6.66)Point 23 (3.49, 6.81)Point 24 (3.66, 7.03)Point 25 (3.75, 6.55)Point 26 (3.62, 6.91)Point 27 (3.24, 6.84)Point 28 (3.09, 6.44)Point 29 (2.73, 6.65)Point 30 (2.64, 6.81)Point 31 (2.5, 5.94)Point 32 (2.26, 6.56)Point 33 (2.14, 6.01)Point 34 (2.13, 5.91)Point 35 (2.14, 6.13)Point 36 (1.98, 6.41)Point 37 (1.71, 5.72)Point 38 (1.6, 5.51)Point 39 (1.29, 5.15)Point 40 (1.51, 5.34)Point 41 (1.52, 5.32)Point 42 (1.16, 5.15)Point 43 (1.34, 4.93)Point 44 (1.61, 4.42)Point 45 (1.22, 4.61)Point 46 (4.06, 5.13)Point 47 (3.74, 4.91)Point 48 (3.99, 4.95)Point 49 (4.04, 4.96)Point 50 (3.88, 5.17)Point 51 (3.96, 4.69)Point 52 (4.14, 4.91)Point 53 (4.26, 4.24)Point 54 (4.17, 4.02)Point 55 (4.08, 4.09)Point 56 (4.04, 3.98)Point 57 (4.32, 4.07)Point 58 (4.94, 3.74)Point 59 (4.6, 3.93)Point 60 (5.13, 3.4)Point 61 (4.87, 3.3)Point 62 (5.65, 3.79)Point 63 (5.38, 3.43)Point 64 (5.48, 3.27)Point 65 (5.63, 3.19)Point 66 (6.35, 3.16)Point 67 (5.91, 3.05)Point 68 (6.11, 2.99)Point 69 (6.38, 3.15)Point 70 (6.9, 3.2)Point 71 (6.61, 3.02)Point 72 (7.01, 3.39)Point 73 (7.01, 3.56)Point 74 (6.96, 3.59)Point 75 (7.48, 3.52)Point 76 (7.88, 3.61)Point 77 (7.16, 3.59)Point 78 (7.62, 3.7)Point 79 (8.05, 3.8)Point 80 (8.16, 3.93)Point 81 (8.07, 4.17)Point 82 (8.44, 4.17)Point 83 (8.02, 4.28)Point 84 (8.22, 4.41)Point 85 (8.28, 4.58)Point 86 (8.43, 4.85)Point 87 (8.3, 5.1)Point 88 (8.47, 5.05)Point 89 (8.83, 5.55)Point 90 (8.28, 5.39)

Complete

78%
02468100246810Point 1 (6.23, 4.31)Point 2 (5.99, 4.67)Point 3 (6.13, 4.9)Point 4 (5.79, 4.89)Point 5 (5.92, 5.31)Point 6 (6.13, 5.38)Point 7 (5.93, 5.47)Point 8 (5.95, 5.42)Point 9 (6.08, 5.99)Point 10 (6.05, 5.8)Point 11 (5.79, 6.02)Point 12 (5.3, 6.34)Point 13 (5.22, 6.13)Point 14 (5.28, 6.15)Point 15 (4.98, 6.61)Point 16 (5.19, 6.64)Point 17 (5.03, 6.49)Point 18 (4.85, 6.92)Point 19 (4.72, 6.21)Point 20 (4.67, 6.63)Point 21 (3.83, 7.11)Point 22 (4.08, 6.66)Point 23 (3.49, 6.81)Point 24 (3.66, 7.03)Point 25 (3.75, 6.55)Point 26 (3.62, 6.91)Point 27 (3.24, 6.84)Point 28 (3.09, 6.44)Point 29 (2.73, 6.65)Point 30 (2.64, 6.81)Point 31 (2.5, 5.94)Point 32 (2.26, 6.56)Point 33 (2.14, 6.01)Point 34 (2.13, 5.91)Point 35 (2.14, 6.13)Point 36 (1.98, 6.41)Point 37 (1.71, 5.72)Point 38 (1.6, 5.51)Point 39 (1.29, 5.15)Point 40 (1.51, 5.34)Point 41 (1.52, 5.32)Point 42 (1.16, 5.15)Point 43 (1.34, 4.93)Point 44 (1.61, 4.42)Point 45 (1.22, 4.61)Point 46 (4.06, 5.13)Point 47 (3.74, 4.91)Point 48 (3.99, 4.95)Point 49 (4.04, 4.96)Point 50 (3.88, 5.17)Point 51 (3.96, 4.69)Point 52 (4.14, 4.91)Point 53 (4.26, 4.24)Point 54 (4.17, 4.02)Point 55 (4.08, 4.09)Point 56 (4.04, 3.98)Point 57 (4.32, 4.07)Point 58 (4.94, 3.74)Point 59 (4.6, 3.93)Point 60 (5.13, 3.4)Point 61 (4.87, 3.3)Point 62 (5.65, 3.79)Point 63 (5.38, 3.43)Point 64 (5.48, 3.27)Point 65 (5.63, 3.19)Point 66 (6.35, 3.16)Point 67 (5.91, 3.05)Point 68 (6.11, 2.99)Point 69 (6.38, 3.15)Point 70 (6.9, 3.2)Point 71 (6.61, 3.02)Point 72 (7.01, 3.39)Point 73 (7.01, 3.56)Point 74 (6.96, 3.59)Point 75 (7.48, 3.52)Point 76 (7.88, 3.61)Point 77 (7.16, 3.59)Point 78 (7.62, 3.7)Point 79 (8.05, 3.8)Point 80 (8.16, 3.93)Point 81 (8.07, 4.17)Point 82 (8.44, 4.17)Point 83 (8.02, 4.28)Point 84 (8.22, 4.41)Point 85 (8.28, 4.58)Point 86 (8.43, 4.85)Point 87 (8.3, 5.1)Point 88 (8.47, 5.05)Point 89 (8.83, 5.55)Point 90 (8.28, 5.39)

Average

78%
02468100246810Point 1 (6.23, 4.31)Point 2 (5.99, 4.67)Point 3 (6.13, 4.9)Point 4 (5.79, 4.89)Point 5 (5.92, 5.31)Point 6 (6.13, 5.38)Point 7 (5.93, 5.47)Point 8 (5.95, 5.42)Point 9 (6.08, 5.99)Point 10 (6.05, 5.8)Point 11 (5.79, 6.02)Point 12 (5.3, 6.34)Point 13 (5.22, 6.13)Point 14 (5.28, 6.15)Point 15 (4.98, 6.61)Point 16 (5.19, 6.64)Point 17 (5.03, 6.49)Point 18 (4.85, 6.92)Point 19 (4.72, 6.21)Point 20 (4.67, 6.63)Point 21 (3.83, 7.11)Point 22 (4.08, 6.66)Point 23 (3.49, 6.81)Point 24 (3.66, 7.03)Point 25 (3.75, 6.55)Point 26 (3.62, 6.91)Point 27 (3.24, 6.84)Point 28 (3.09, 6.44)Point 29 (2.73, 6.65)Point 30 (2.64, 6.81)Point 31 (2.5, 5.94)Point 32 (2.26, 6.56)Point 33 (2.14, 6.01)Point 34 (2.13, 5.91)Point 35 (2.14, 6.13)Point 36 (1.98, 6.41)Point 37 (1.71, 5.72)Point 38 (1.6, 5.51)Point 39 (1.29, 5.15)Point 40 (1.51, 5.34)Point 41 (1.52, 5.32)Point 42 (1.16, 5.15)Point 43 (1.34, 4.93)Point 44 (1.61, 4.42)Point 45 (1.22, 4.61)Point 46 (4.06, 5.13)Point 47 (3.74, 4.91)Point 48 (3.99, 4.95)Point 49 (4.04, 4.96)Point 50 (3.88, 5.17)Point 51 (3.96, 4.69)Point 52 (4.14, 4.91)Point 53 (4.26, 4.24)Point 54 (4.17, 4.02)Point 55 (4.08, 4.09)Point 56 (4.04, 3.98)Point 57 (4.32, 4.07)Point 58 (4.94, 3.74)Point 59 (4.6, 3.93)Point 60 (5.13, 3.4)Point 61 (4.87, 3.3)Point 62 (5.65, 3.79)Point 63 (5.38, 3.43)Point 64 (5.48, 3.27)Point 65 (5.63, 3.19)Point 66 (6.35, 3.16)Point 67 (5.91, 3.05)Point 68 (6.11, 2.99)Point 69 (6.38, 3.15)Point 70 (6.9, 3.2)Point 71 (6.61, 3.02)Point 72 (7.01, 3.39)Point 73 (7.01, 3.56)Point 74 (6.96, 3.59)Point 75 (7.48, 3.52)Point 76 (7.88, 3.61)Point 77 (7.16, 3.59)Point 78 (7.62, 3.7)Point 79 (8.05, 3.8)Point 80 (8.16, 3.93)Point 81 (8.07, 4.17)Point 82 (8.44, 4.17)Point 83 (8.02, 4.28)Point 84 (8.22, 4.41)Point 85 (8.28, 4.58)Point 86 (8.43, 4.85)Point 87 (8.3, 5.1)Point 88 (8.47, 5.05)Point 89 (8.83, 5.55)Point 90 (8.28, 5.39)

Ward

84%
02468100246810Point 1 (6.23, 4.31)Point 2 (5.99, 4.67)Point 3 (6.13, 4.9)Point 4 (5.79, 4.89)Point 5 (5.92, 5.31)Point 6 (6.13, 5.38)Point 7 (5.93, 5.47)Point 8 (5.95, 5.42)Point 9 (6.08, 5.99)Point 10 (6.05, 5.8)Point 11 (5.79, 6.02)Point 12 (5.3, 6.34)Point 13 (5.22, 6.13)Point 14 (5.28, 6.15)Point 15 (4.98, 6.61)Point 16 (5.19, 6.64)Point 17 (5.03, 6.49)Point 18 (4.85, 6.92)Point 19 (4.72, 6.21)Point 20 (4.67, 6.63)Point 21 (3.83, 7.11)Point 22 (4.08, 6.66)Point 23 (3.49, 6.81)Point 24 (3.66, 7.03)Point 25 (3.75, 6.55)Point 26 (3.62, 6.91)Point 27 (3.24, 6.84)Point 28 (3.09, 6.44)Point 29 (2.73, 6.65)Point 30 (2.64, 6.81)Point 31 (2.5, 5.94)Point 32 (2.26, 6.56)Point 33 (2.14, 6.01)Point 34 (2.13, 5.91)Point 35 (2.14, 6.13)Point 36 (1.98, 6.41)Point 37 (1.71, 5.72)Point 38 (1.6, 5.51)Point 39 (1.29, 5.15)Point 40 (1.51, 5.34)Point 41 (1.52, 5.32)Point 42 (1.16, 5.15)Point 43 (1.34, 4.93)Point 44 (1.61, 4.42)Point 45 (1.22, 4.61)Point 46 (4.06, 5.13)Point 47 (3.74, 4.91)Point 48 (3.99, 4.95)Point 49 (4.04, 4.96)Point 50 (3.88, 5.17)Point 51 (3.96, 4.69)Point 52 (4.14, 4.91)Point 53 (4.26, 4.24)Point 54 (4.17, 4.02)Point 55 (4.08, 4.09)Point 56 (4.04, 3.98)Point 57 (4.32, 4.07)Point 58 (4.94, 3.74)Point 59 (4.6, 3.93)Point 60 (5.13, 3.4)Point 61 (4.87, 3.3)Point 62 (5.65, 3.79)Point 63 (5.38, 3.43)Point 64 (5.48, 3.27)Point 65 (5.63, 3.19)Point 66 (6.35, 3.16)Point 67 (5.91, 3.05)Point 68 (6.11, 2.99)Point 69 (6.38, 3.15)Point 70 (6.9, 3.2)Point 71 (6.61, 3.02)Point 72 (7.01, 3.39)Point 73 (7.01, 3.56)Point 74 (6.96, 3.59)Point 75 (7.48, 3.52)Point 76 (7.88, 3.61)Point 77 (7.16, 3.59)Point 78 (7.62, 3.7)Point 79 (8.05, 3.8)Point 80 (8.16, 3.93)Point 81 (8.07, 4.17)Point 82 (8.44, 4.17)Point 83 (8.02, 4.28)Point 84 (8.22, 4.41)Point 85 (8.28, 4.58)Point 86 (8.43, 4.85)Point 87 (8.3, 5.1)Point 88 (8.47, 5.05)Point 89 (8.83, 5.55)Point 90 (8.28, 5.39)

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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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?”.
time DBSCAN O(n log n) with a spatial index, O(n²) without; agglomerative O(n²) to O(n³)space DBSCAN O(n); agglomerative O(n²)

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.