Skip to content

Density-based clusters: DBSCAN, HDBSCAN and OPTICS

Clusters as dense regions of any shape, with noise as a real answer. Learn to set the radius and when to let HDBSCAN or OPTICS handle varying density.

Applied ~12 min9 steps

What runs in CodeSplash

You will be able to

  • Explain core points, reachability and noise
  • Choose eps from a k-distance plot
  • Decide between DBSCAN, HDBSCAN and OPTICS when density varies

Read first

01

A different idea of a cluster

Read

k-means asks for kk round groups and puts every row in one. DBSCANDBSCANGrows clusters from rows with enough neighbors inside a radius, and labels everything left over as noise. asks a different question: which regions are dense?

A row is a core point if at least min_samples rows lie within radius eps. Core points that reach each other form a cluster, and rows near a core point join it. Everything else is noise.

So you don't choose kk, clusters can take any shape, and "this row belongs nowhere" is an allowed answer. The app labels noise as −1.

02

Choose the radius

Explore

AExplorable · DBSCAN: choosing eps

How close is close enough for points to belong together?

How many clusters will DBSCAN find at this radius? You didn't have to tell it.

6
How sure?

Data

0.08
5
250
Simulating…

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

On Two moons, find a radius that recovers both moons with under 5% noise. Now try One tight, one loose: any single radius either shatters the loose cluster or merges too much. That failure is the reason HDBSCAN exists.

03

Make a guess

Predict

Predict first

On iris, the suggested radius leaves few rows as noise. What happens to noise if you pick a smaller, fixed radius?

More noiseLess noiseNo change
Reveal

More noise. A smaller radius makes fewer points core. The suggested radius of 0.757 labels 5 rows as noise. A fixed radius of 0.600 labels 26.

04

The k-distance plot

Read

Leave eps empty and the app suggests one with the k-distance plot: sort rows by the distance to their min_samples-th neighbor and find the knee.

Rows left of the knee sit inside dense regions. Rows right of it are increasingly isolated, so the knee marks a natural radius.

k-distance curveWhich neighborhood radius (eps) separates dense rows from sparse ones?
View data
rows (sorted)distance to the 5th neighborchosen eps
10.1637false
20.1639false
30.1653false
40.1683false
50.1722false
60.1727false
70.1737false
80.1738false
90.1744false
100.1747false
110.1764false
120.1793false
130.1801false
140.1802false
150.1827false
160.1828false
170.1833false
180.1839false
190.1849false
200.1857false
210.1857false
220.1858false
230.1879false
240.1906false
250.1907false
260.1907false
270.1920false
280.1920false
290.1922false
300.1923false
310.1926false
320.1946false
330.1953false
340.1959false
350.1969false
360.1973false
370.1975false
380.1986false
390.2001false
400.2005false
410.2026false
420.2038false
430.2045false
440.2066false
450.2080false
460.2088false
470.2096false
480.2101false
490.2122false
500.2135false
510.2138false
520.2140false
530.2152false
540.2171false
550.2205false
560.2223false
570.2232false
580.2234false
590.2241false
600.2272false

First 60 of 300 rows.

A knee is a starting point. If the curve bends gradually, density varies and no single radius will suit every group.

05

When density varies

Read

HDBSCANHDBSCANTries every density level and keeps the clusters that persist longest, so groups may differ in density. tries every radius at once and keeps the clusters that persist longest, so you set only min_cluster_size. On iris with a minimum size of 8, it finds 2 clusters and 2 noise rows.

OPTICSOPTICSOrders rows so dense regions sit together and records reachability distances — every DBSCAN radius at once. orders the rows so that dense regions become valleys in a reachability plot. You read the structure first, then choose a cut.

Reachability plot (OPTICS)Where are the dense regions, at every density at once?
View data
OPTICS orderreachability distanceclusterno reachability
11.6001true
20.32101false
30.33801false
40.28601false
50.34801false
60.28501false
70.34901false
80.33101false
90.31801false
100.34901false
110.26501false
120.34301false
130.37001false
140.31501false
150.28101false
160.36701false
170.27301false
180.25901false
190.27401false
200.29601false
210.32101false
220.34301false
230.26301false
240.32301false
250.29101false
260.25201false
270.26801false
280.30301false
290.25101false
300.27101false
310.27101false
320.29001false
330.30701false
340.34901false
350.29201false
360.27801false
370.25601false
380.28001false
390.34501false
400.41201false
410.32801false
420.38801false
430.25901false
440.25101false
450.26801false
460.26401false
470.29001false
480.30901false
490.27601false
500.25301false
510.28501false
520.36101false
530.28301false
540.39701false
550.26101false
560.32601false
570.26501false
580.26701false
590.41201false
600.30201false

First 60 of 157 rows.

On iris all three find two clusters. Two of the species overlap in density, so density alone can't separate them, whatever the label says.

06

Noise is a finding

Reflect

HDBSCAN on air quality labels 102 of 153 days as noise. Its silhouette of 0.471 covers only the clustered rows.

So a density method can look excellent by keeping a few tight clusters and discarding most rows. Always report the noise count beside any quality score.

A colleague shows you a high silhouette from HDBSCAN and doesn't mention noise. What do you ask before you believe it?

07

Check your understanding

Checkpoint

Check yourself

What makes a row a core point in DBSCAN?

Show the answer

Answer: b

Core status is local. Clusters grow by connecting core points that are within eps of each other, so they can curve.

Check yourself

You have one dense cluster and one sparse cluster. Which method is the better fit?

Show the answer

Answer: b

A single radius has to suit both groups. HDBSCAN explores all radii and keeps the clusters that persist.

Check yourself

A run reports a silhouette of 0.7 and labels 60% of rows as noise. What's the honest summary?

Show the answer

Answer: b

Silhouette here covers clustered rows only. Report both numbers, then decide whether a method that leaves most rows out helps you.

08

Do it on your own data

In the app

Under Find segments, try DBSCAN first with eps left empty, then compare with HDBSCAN on the same features.

Python
from sklearn.cluster import DBSCAN, HDBSCAN
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler

X = StandardScaler().fit_transform(load_iris().data)
db = DBSCAN(eps=0.6, min_samples=5).fit(X)
print("DBSCAN", db.labels_.max() + 1, (db.labels_ == -1).sum())
hd = HDBSCAN(min_cluster_size=8).fit(X)
print("HDBSCAN", hd.labels_.max() + 1, (hd.labels_ == -1).sum())
R
X <- scale(iris[, 1:4])
db <- dbscan::dbscan(X, eps = 0.6, minPts = 5)
c(clusters = max(db$cluster), noise = sum(db$cluster == 0))
hd <- dbscan::hdbscan(X, minPts = 8)
c(clusters = max(hd$cluster), noise = sum(hd$cluster == 0))

Note the trap between languages: scikit-learn marks noise as −1, while R's dbscan package uses 0.

09

Recap

Recap
  • Density clusters take any shape, and noise is a legitimate answer.
  • The k-distance knee suggests eps; HDBSCAN and OPTICS handle varying density.
  • Always report noise beside any quality score.