DBSCAN Density Clustering
DBSCAN finds clusters by looking for regions where points are packed densely together, letting clusters take any shape and automatically labelling sparse, isolated points as noise instead of forcing them into a group.
Prerequisites: K-Means Clustering
K-means clustering assumes clusters are round blobs and forces every single point into one of a fixed number of groups, even the stray point that clearly belongs to no group at all. Real data is rarely so obliging — clusters can be curved, elongated, or wrap around each other, and some points genuinely are noise that shouldn't be forced anywhere. DBSCAN clusters by a different rule entirely: not "which centre is closest" but "how crowded is the neighbourhood around me."
The analogy: finding crowds in a stadium from above
Imagine looking down at a stadium's crowd from a drone. Dense knots of people standing shoulder to shoulder are clearly "a group," regardless of what shape that group traces on the ground — a group could be a long snaking queue, a ring around a vendor, or a blob. Meanwhile a lone person standing far from everyone else is obviously not part of any group. You don't need to know how many groups there are in advance, and you don't need every person to belong to one — you just need a rule for "how many people nearby counts as a crowd." DBSCAN applies exactly this rule to data points instead of people.
The maths: core points, reachability, and noise
DBSCAN needs two parameters: , a distance defining "nearby," and , the number of neighbours required to count as dense. A point is a core point if it has at least neighbours within distance :
In words: count how many other points sit within a small radius of ; if that count clears the threshold, is sitting in a dense region. A cluster is then built by chaining core points together: any core point directly reachable from another core point joins the same cluster, and non-core points that fall within of a core point are added to that cluster as border members without needing density of their own. Any point that is neither a core point nor within of one is labelled noise — it belongs to nothing.
Worked example 1: three points, minPts = 2
Points at positions on a line. Set , . Check point : neighbours within are and (distances and ), so it has 2 neighbours — that meets , so is a core point. Point : only neighbour within is (distance ); that's 1 neighbour, just short of on its own, but since falls within of core point , it joins as a border point. Same logic makes a border point of the same cluster. Point has no neighbours within of anything — it's noise. Result: one cluster , one noise point , with no need to have specified "2 clusters" in advance.
Worked example 2: why shape doesn't matter
Consider points tracing a thin crescent shape plus a small round blob nested inside the crescent's curve. K-means with , which only understands round clusters built around a centre point, would slice straight through both shapes and badly misassign points — a centre placed in the middle of the crescent isn't actually close to most of the crescent's own points. DBSCAN instead walks along the crescent: each point along the arc has enough densely-packed neighbours along the arc to count as core, chaining the whole crescent into one cluster regardless of its curve, while the round blob's points chain into a separate cluster by the same local-density logic. Neither cluster ever needed to be "round" or centred anywhere — density decided the shape, not distance-to-a-centre.
The explorer above shows how classifier flexibility changes what regions get grouped together — DBSCAN's clusters are the unsupervised analogue: dense regions get outlined in whatever shape they actually take, not forced into a preset geometry.
Density-based membership is a threshold effect, not unlike the steep transition of a logistic curve shown here — a point is either "in a dense enough neighbourhood" or it isn't, with the /minPts pair setting where that line falls.
DBSCAN groups points by local density rather than distance to a centre, which lets clusters take any shape and automatically flags sparse points as noise instead of forcing them into the nearest group.
What this means in practice
DBSCAN is useful wherever clusters aren't round and where "not everything belongs to a group" is a real possibility — detecting clusters of correlated trading anomalies, grouping similar market regimes from irregularly shaped feature clouds, or finding genuine outlier trades that shouldn't be lumped into any behavioural cluster. Its Achilles' heel is a single global : if clusters in the data have very different densities, one that works for the dense cluster will swallow the sparse one into noise, or vice versa.
The classic mistake is treating and as safe defaults rather than data-dependent choices. Too small an turns almost everything into noise; too large an merges genuinely separate clusters into one blob. Unlike k-means, DBSCAN doesn't need you to guess the number of clusters — but it trades that for needing you to guess the right notion of "nearby," which is not obviously easier.
Related concepts
Practice in interviews
Further reading
- Ester, Kriegel, Sander & Xu, A Density-Based Algorithm for Discovering Clusters (1996)
- Hastie, Tibshirani & Friedman, The Elements of Statistical Learning, ch. 14.3