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.
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
A different idea of a cluster
Readk-means asks for 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 , clusters can take any shape, and "this row belongs nowhere" is an allowed answer. The app labels noise as −1.
Choose the radius
ExploreAExplorable · 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.
Data
Commit your prediction above to see the result
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.
Make a guess
PredictPredict first
On iris, the suggested radius leaves few rows as noise. What happens to noise if you pick a smaller, fixed radius?
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.
The k-distance plot
ReadLeave 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.
View data
| rows (sorted) | distance to the 5th neighbor | chosen eps |
|---|---|---|
| 1 | 0.1637 | false |
| 2 | 0.1639 | false |
| 3 | 0.1653 | false |
| 4 | 0.1683 | false |
| 5 | 0.1722 | false |
| 6 | 0.1727 | false |
| 7 | 0.1737 | false |
| 8 | 0.1738 | false |
| 9 | 0.1744 | false |
| 10 | 0.1747 | false |
| 11 | 0.1764 | false |
| 12 | 0.1793 | false |
| 13 | 0.1801 | false |
| 14 | 0.1802 | false |
| 15 | 0.1827 | false |
| 16 | 0.1828 | false |
| 17 | 0.1833 | false |
| 18 | 0.1839 | false |
| 19 | 0.1849 | false |
| 20 | 0.1857 | false |
| 21 | 0.1857 | false |
| 22 | 0.1858 | false |
| 23 | 0.1879 | false |
| 24 | 0.1906 | false |
| 25 | 0.1907 | false |
| 26 | 0.1907 | false |
| 27 | 0.1920 | false |
| 28 | 0.1920 | false |
| 29 | 0.1922 | false |
| 30 | 0.1923 | false |
| 31 | 0.1926 | false |
| 32 | 0.1946 | false |
| 33 | 0.1953 | false |
| 34 | 0.1959 | false |
| 35 | 0.1969 | false |
| 36 | 0.1973 | false |
| 37 | 0.1975 | false |
| 38 | 0.1986 | false |
| 39 | 0.2001 | false |
| 40 | 0.2005 | false |
| 41 | 0.2026 | false |
| 42 | 0.2038 | false |
| 43 | 0.2045 | false |
| 44 | 0.2066 | false |
| 45 | 0.2080 | false |
| 46 | 0.2088 | false |
| 47 | 0.2096 | false |
| 48 | 0.2101 | false |
| 49 | 0.2122 | false |
| 50 | 0.2135 | false |
| 51 | 0.2138 | false |
| 52 | 0.2140 | false |
| 53 | 0.2152 | false |
| 54 | 0.2171 | false |
| 55 | 0.2205 | false |
| 56 | 0.2223 | false |
| 57 | 0.2232 | false |
| 58 | 0.2234 | false |
| 59 | 0.2241 | false |
| 60 | 0.2272 | false |
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.
When density varies
ReadHDBSCANHDBSCANTries 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.
View data
| OPTICS order | reachability distance | cluster | no reachability |
|---|---|---|---|
| 1 | 1.600 | 1 | true |
| 2 | 0.3210 | 1 | false |
| 3 | 0.3380 | 1 | false |
| 4 | 0.2860 | 1 | false |
| 5 | 0.3480 | 1 | false |
| 6 | 0.2850 | 1 | false |
| 7 | 0.3490 | 1 | false |
| 8 | 0.3310 | 1 | false |
| 9 | 0.3180 | 1 | false |
| 10 | 0.3490 | 1 | false |
| 11 | 0.2650 | 1 | false |
| 12 | 0.3430 | 1 | false |
| 13 | 0.3700 | 1 | false |
| 14 | 0.3150 | 1 | false |
| 15 | 0.2810 | 1 | false |
| 16 | 0.3670 | 1 | false |
| 17 | 0.2730 | 1 | false |
| 18 | 0.2590 | 1 | false |
| 19 | 0.2740 | 1 | false |
| 20 | 0.2960 | 1 | false |
| 21 | 0.3210 | 1 | false |
| 22 | 0.3430 | 1 | false |
| 23 | 0.2630 | 1 | false |
| 24 | 0.3230 | 1 | false |
| 25 | 0.2910 | 1 | false |
| 26 | 0.2520 | 1 | false |
| 27 | 0.2680 | 1 | false |
| 28 | 0.3030 | 1 | false |
| 29 | 0.2510 | 1 | false |
| 30 | 0.2710 | 1 | false |
| 31 | 0.2710 | 1 | false |
| 32 | 0.2900 | 1 | false |
| 33 | 0.3070 | 1 | false |
| 34 | 0.3490 | 1 | false |
| 35 | 0.2920 | 1 | false |
| 36 | 0.2780 | 1 | false |
| 37 | 0.2560 | 1 | false |
| 38 | 0.2800 | 1 | false |
| 39 | 0.3450 | 1 | false |
| 40 | 0.4120 | 1 | false |
| 41 | 0.3280 | 1 | false |
| 42 | 0.3880 | 1 | false |
| 43 | 0.2590 | 1 | false |
| 44 | 0.2510 | 1 | false |
| 45 | 0.2680 | 1 | false |
| 46 | 0.2640 | 1 | false |
| 47 | 0.2900 | 1 | false |
| 48 | 0.3090 | 1 | false |
| 49 | 0.2760 | 1 | false |
| 50 | 0.2530 | 1 | false |
| 51 | 0.2850 | 1 | false |
| 52 | 0.3610 | 1 | false |
| 53 | 0.2830 | 1 | false |
| 54 | 0.3970 | 1 | false |
| 55 | 0.2610 | 1 | false |
| 56 | 0.3260 | 1 | false |
| 57 | 0.2650 | 1 | false |
| 58 | 0.2670 | 1 | false |
| 59 | 0.4120 | 1 | false |
| 60 | 0.3020 | 1 | false |
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.
Noise is a finding
ReflectHDBSCAN 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?
Check your understanding
CheckpointCheck 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.
Do it on your own data
In the appUnder Find segments, try DBSCAN first with eps left empty, then compare with HDBSCAN on the same features.
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())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.
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.