[Clustering]
DBSCAN.
Density-based clustering that finds arbitrarily shaped clusters.
SPEC SHEET
FIG — THE MECHANISM, LIVE
Density-based clustering finds the same groups - but without being told how many to look for.
C — How it actually works
A cluster is anywhere the data is dense. Pick a radius (eps) and a minimum crowd size (min_samples); points with enough neighbours are "core", chains of touching core points grow into clusters of any shape, and points near no crowd are labelled noise. You never say how many clusters - the density decides.
D — The math
Core point: ≥ min_samples within radius eps. Clusters are the connected components of density-reachability; everything else is noise (label −1).
Training
O(n log n) with a spatial index, O(n²) worst case
Inference
no native predict - assign to nearest core point
E — When NOT to use it
- Clusters of very different densities - one eps cannot fit both (use HDBSCAN)
- High-dimensional data where distance concentrates and density stops meaning anything
- You need a clean predict() for new points in production
F — Tuning that matters
- Set min_samples ≈ 2·d as a floor, then choose eps from the "knee" of the sorted k-distance plot
- Standardise features first - eps is a single global radius
- If clusters vary in density, go straight to HDBSCAN instead of grid-searching eps
- Check the noise fraction: 5-15% is often healthy, 60% means eps is too small
G — Production pitfalls
- Grid-searching eps blindly instead of reading the k-distance plot
- Interpreting noise points as errors - they are frequently the interesting anomalies
- Using Euclidean eps on mixed-scale or categorical features
- Expecting stable clusters when density varies across regions
H — Minimal starting point
PYTHONfrom sklearn.cluster import DBSCAN
from sklearn.preprocessing import StandardScaler
X = StandardScaler().fit_transform(X_raw)
db = DBSCAN(eps=0.6, min_samples=10).fit(X)
labels = db.labels_ # -1 = noise
n_clusters = len(set(labels)) - (1 if -1 in labels else 0)I — The interview question
How does DBSCAN find arbitrarily-shaped clusters when k-means cannot?
k-means assigns by distance to a centroid, which forces convex, blobby regions. DBSCAN grows clusters by chaining locally dense neighbourhoods, so any connected dense shape - rings, spirals, snakes - emerges naturally, and sparse points become explicit noise.
J — In the wild
GPS mobility studies cluster millions of location pings with DBSCAN to discover "stay points" - homes, offices, gyms - because places are dense blobs of arbitrary shape and the sparse points in between are exactly the travel to discard.
K — Consider instead
- K-Means Clustering— when blobs are roughly spherical and you need speed + a predict method
- Hierarchical Clustering— when you want to explore structure at multiple granularities