Skip to content
Lambdian

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 kk 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 O(nlog⁡n)O(n\log n).

The algorithm, step by step

  1. 1
    For every point, find its ε-neighbours; mark core points (≥ minPts neighbours).
  2. 2
    Pick an unvisited core point and start a new cluster with it.
  3. 3
    Expand: add all its neighbours; for every newly added core point, add its neighbours too (breadth-first).
  4. 4
    Repeat from another unvisited core point; points never reached are noise (label −1).

The maths

ε-neighbourhood

Nε(p)={q∈D:∥p−q∥≤ε}N_\varepsilon(p) = \{q\in D : \lVert p - q\rVert \le \varepsilon\}

Core condition

∣Nε(p)∣≥minPts|N_\varepsilon(p)| \ge \text{minPts}

k-distance heuristic for ε

ε≈knee of sort⁡p dk(p),k=minPts\varepsilon \approx \text{knee of } \operatorname{sort}_p\, d_k(p),\quad k = \text{minPts}

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…

Spatial or low-dimensional data with irregular cluster shapes and noise: GPS points, anomaly detection, image segmentation.

Check your understanding

Question 1

Q1A point has 2 neighbours within ε (minPts = 5) but lies within ε of a core point. It is…

Question 2

Q2Which can DBSCAN do that k-means cannot?

Question 3

Q3If ε is set far too large…

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.
Open the labPython + NumPy + scikit-learn runs in your browser. Sign-in keeps your code and attempts.