DBSCAN (density-based clustering)
Clusters as dense regions: arbitrary shapes, no k, and outliers labelled as noise.
Animated explanation
The idea
Look at a map of city lights at night: towns are dense patches, separated by dark countryside with a few isolated houses. DBSCAN clusters the same way. A point in a dense spot (at least minPts neighbours within radius ε) is a core point. Core points that are close to each other link up into one cluster, which can snake into any shape. Points near a cluster but not dense themselves join it as border points. Everything else is noise.
There is no to choose and no assumption that clusters are round, which makes it ideal for spatial data and for finding outliers. The price: one density threshold for the whole dataset, so clusters of very different densities are hard.
Choosing ε and minPts
A common rule: minPts ≈ 2 × dimensions (at least 4). Then compute every point's distance to its minPts-th nearest neighbour, sort these distances, and plot them: the knee of this k-distance curve is a good ε. Scale features first, since ε is a distance.
Relatives
HDBSCAN runs DBSCAN over all ε values and keeps the most stable clusters, handling varying densities with only a minimum cluster size to set. OPTICS orders points by reachability so clusters at different densities can be read off one plot. With a spatial index, DBSCAN costs about .
The algorithm, step by step
- 1For every point, find its ε-neighbours; mark core points (≥ minPts neighbours).
- 2Pick an unvisited core point and start a new cluster with it.
- 3Expand: add all its neighbours; for every newly added core point, add its neighbours too (breadth-first).
- 4Repeat from another unvisited core point; points never reached are noise (label −1).
The maths
ε-neighbourhood
Core condition
k-distance heuristic for ε
Strengths and weaknesses
Strengths
- Finds arbitrarily shaped clusters
- Labels outliers as noise
- No need to choose the number of clusters
Weaknesses
- One global density: struggles with varying densities
- Sensitive to ε; needs scaled features
- Weak in high dimensions (distances concentrate)
Reach for it when…
Check your understanding
Lambdian Lab
DBSCAN from scratch
Implement DBSCAN with Euclidean distance (a point counts as its own neighbour).
- region_query(D, i, eps): indices j with D[i, j] <= eps, given a distance matrix D.
- dbscan(X, eps, min_pts): labels with −1 for noise and clusters numbered 0, 1, … in the order they are discovered (scan points in index order).
- k_distance(X, k): each point's distance to its k-th nearest neighbour (itself excluded), sorted ascending.