Interview's MateData Science
Interview's Mate

Data Science

Python → ML → MLOps

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Science topics
Lesson 95/ 160
05. Classical Machine Learning

DBSCAN & Density-Based Clustering

Find arbitrary-shaped clusters and noise points with density connectivity.

Advancedeps/minPts · Core Points3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

DBSCAN groups points that are packed closely together, lets the groups take any shape and marks lonely points as noise.

Think of it like…

Finding towns on a night-time satellite map: clusters of lights close together form a town, a chain of closely spaced lights along a road extends it, and isolated lights in the countryside are noise.

Where you’ve seen it

Eight one-dimensional points: 1.0, 1.2, 1.4, 1.5, 5.0, 5.2, 5.3, 9.0 with minPts = 3. With ε = 0.5, point 1.0 has neighbours 1.0, 1.2, 1.4, 1.5 (1.5 is exactly 0.5 away), so 4 ≥ 3 and it counts as a core point; the first four points form cluster A, points 5.0, 5.2, 5.3 each have 3 neighbours and form cluster B, and 9.0 has only itself, so it is noise. With ε = 0.15 no point has 3 neighbours, so everything is noise; with ε = 4.5 the points chain into one cluster and nothing is noise. For 200,000 taxi pickup coordinates the team uses the haversine metric with ε = 150 m and minPts = 30 to find pickup hotspots, picking ε from the knee of a sorted k-distance plot (distance to each point's 30th neighbour), and uses HDBSCAN where hotspot densities vary between downtown and suburbs.

Words you’ll hear

eps (ε)
The neighbourhood radius that defines closeness.
minPts
The minimum number of points in a neighbourhood for a core point.
Core / border / noise
Dense interior points, edge points attached to a core and unclustered outliers.
k-distance plot
Sorted distances to the k-th nearest neighbour, used to pick eps at the knee.

Visual diagram

Loading interactive diagram

Theory & deep dive

Definitions, algorithm and parameter choice

For a point p, its ε-neighbourhood is all points within distance ε (including p). If it contains at least minPts points, p counts as a core point. A point q is directly density-reachable from p if p is core and q is in p's neighbourhood; clusters are maximal sets of density-connected points. The algorithm scans points: an unvisited core point starts a new cluster and the cluster expands by repeatedly adding the neighbourhoods of core points it reaches; border points join the first cluster that reaches them (so their assignment can depend on processing order), and points reachable from no core point stay noise. With a spatial index (KD-tree, ball tree) the runtime is about O(n log n); without one it is O(n²).

Choose minPts from domain knowledge and dimensionality: at least d + 1, commonly 2 × d (for 2D data 4 to 5, larger such as 20 to 50 for noisy data or big samples). Choose ε with a k-distance plot: compute each point's distance to its minPts-th neighbour (NearestNeighbors), sort ascending and look for the knee, the distance where the curve rises sharply; points beyond the knee are noise candidates. In the eight-point example ε = 0.5 gives two clusters and one noise point, ε = 0.15 gives all noise and ε = 4.5 gives one cluster, which shows how strongly the result depends on ε. Scale features before computing distances, and choose a metric that suits the data (haversine for latitude and longitude, cosine for embeddings).

Core, border and noise at eps 0.5

1.0 has neighbours {1.0, 1.2, 1.4, 1.5} = 4 ≥ 3 → core. 5.0 has {5.0, 5.2, 5.3} = 3 → core. 9.0 has only itself = 1 < 3, and no core point within 0.5 → noise. Clusters: A = {1.0, 1.2, 1.4, 1.5}, B = {5.0, 5.2, 5.3}.

Key takeaway: DBSCAN labels core, border and noise points from eps and minPts; pick eps at the knee of the k-distance plot and scale features.

Strengths, limits and alternatives

Strengths: arbitrary cluster shapes (moons, rings, elongated structures), no need to specify the number of clusters, built-in noise handling (outliers aren't forced into clusters) and robustness to a few outliers; it's also easy to explain in spatial terms ("at least 30 pickups within 150 metres"). Limits: a single global ε can't handle clusters of very different densities (a dense downtown and sparse suburbs: ε that finds suburbs merges downtown into one blob, and ε that splits downtown marks suburbs as noise); results are sensitive to ε and minPts; in high dimensions distances concentrate, so neighbourhoods lose meaning (reduce dimensions first or use a different approach); border-point assignment is order-dependent; and there is no natural way to predict cluster membership for new points besides nearest-core assignment.

Alternatives: HDBSCAN (hierarchical DBSCAN) builds a hierarchy over all ε values and extracts stable clusters, handling varying densities and requiring mainly a minimum cluster size; OPTICS orders points by reachability distance so you can extract clusters at multiple ε; Gaussian mixture models give soft, probabilistic ellipsoidal clusters; k-means is better for compact blobs and large data. Practical advice: visualise the k-distance plot, try a small grid over ε and minPts, check how many points are noise (10 to 30% is common; 80% means ε is too small), validate with domain checks (do hotspots match known venues?) and use silhouette only cautiously (it favours convex clusters). For streaming or very large data use approximate neighbour searches or sampling.

Varying density

Downtown pickups are 10× denser than suburban ones. ε = 150 m finds downtown hotspots but labels most suburban pickups as noise; ε = 600 m finds suburbs but merges downtown into one giant cluster. HDBSCAN extracts both sets by selecting clusters that are stable across a range of density levels.

Key takeaway: DBSCAN finds arbitrary shapes and noise but depends on a single density scale; use HDBSCAN or OPTICS when densities vary.

How it works, step by step

Step 1 of 4
Analyst

1. Prepare and scale

Choose a suitable metric (haversine for GPS), scale features and reduce dimensions if high.

haversine on lat/long
Key numbers at this step
High-d
reduce first

When to use it & trade-offs

What it is

DBSCAN (Density-Based Spatial Clustering of Applications with Noise) defines clusters as dense regions separated by sparse regions. It has two parameters: a radius eps (ε) and a minimum neighbour count minPts. A point counts as a core point if at least minPts points (itself included) lie within ε; points within ε of a core point but not core themselves are border points; everything else is noise. Clusters are grown by chaining core points that are within ε of each other. Unlike k-means, DBSCAN finds arbitrarily shaped clusters, doesn't need the number of clusters in advance and labels outliers explicitly (label −1), but it struggles when densities vary and in very high dimensions.

Why it matters: Many real structures aren't round blobs: geographic hotspots, trajectories, moons and rings, fraud rings. DBSCAN recovers those shapes and flags noise in the same pass, which makes it a go-to method for spatial data, anomaly screening and cleaning. It also teaches an important lesson: parameter choice (ε) encodes what "dense" means for your data, and a bad value gives either all noise or one giant cluster.

When to reach for it

  • Geospatial hotspots and trajectories
  • Outlier-aware clustering and data cleaning
  • Clusters of unknown number and non-spherical shape

When to avoid or be careful

  • A single eps across very different densities
  • Raw high-dimensional features without reduction
  • Unscaled mixed-unit features

Density-based and other clustering

DBSCAN
Best for: Arbitrary shapes with noise at a roughly uniform density

Strengths: No k needed; finds outliers; non-convex clusters.

Watch out: Single eps; sensitive parameters; weak in high dimensions.

HDBSCAN
Best for: Clusters with different densities

Strengths: Hierarchical; robust to eps; stable clusters.

Watch out: More complex; extra dependency.

K-means
Best for: Large data with compact clusters

Strengths: Fast and simple.

Watch out: Spherical assumption; no noise label.

Interview signals & numbers to know

When this topic comes up

  • “Clusters aren't round”
  • “Find hotspots and outliers”
  • eps and minPts
  • k-distance plot
  • HDBSCAN

Benchmarks & rules of thumb

≥ d + 1 or 2d -
common minPts choices
O(n log n) -
runtime with a spatial index
eps 0.15 / 0.5 / 4.5 -
all noise, two clusters, one cluster in the example

Common mistakes & follow-ups

Common interview mistakes

  • Taking eps from guesswork instead of the k-distance plot.
  • Treating noise points as a defect rather than a result.
  • Applying Euclidean DBSCAN to raw latitude and longitude.

Likely interviewer follow-ups

  • How does DBSCAN decide what is noise?
  • How would you choose eps and minPts?
  • Why does DBSCAN struggle with varying densities?
Interview tip: Walk through the eps = 0.15, 0.5 and 4.5 outcomes on the eight points. It makes the sensitivity to eps immediate.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply DBSCAN & Density-Based Clustering with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
K-Means & Hierarchical Clustering
Next topic
Principal Component Analysis (PCA)