Sub topic 17
Going further: OPTICS and HDBSCAN
This chapter assumes you can run DBSCAN in scikit-learn and know what eps and min_samples do. It also assumes you have seen core, border, and noise points. Here you will see two methods that relax DBSCAN's biggest restriction: a single global density threshold.
The problem both methods address
DBSCAN uses one eps for the whole dataset. If one cluster is tight and another is loose, no single eps fits both. A small value splits the loose cluster into pieces or labels it noise. A large value merges the tight clusters with their neighbors.
OPTICS and HDBSCAN handle this by looking at many density levels instead of one.
OPTICS: an ordering instead of a labeling
OPTICS (Ordering Points To Identify the Clustering Structure) does not commit to one eps. It sets a generous upper bound, max_eps, and visits points in an order that keeps neighbors close together. For each point it records a reachability distance, which is roughly how far you must travel from the already-visited dense region to reach that point.
Plotting reachability in visiting order gives a reachability plot. Valleys are dense clusters. Peaks are the gaps between them. Deep, narrow valleys are tight clusters and shallow, wide valleys are loose ones, so a single plot shows clusters of different densities.
from sklearn.cluster import OPTICS
from sklearn.datasets import make_blobs
from sklearn.preprocessing import StandardScaler
X, _ = make_blobs(
n_samples=[200, 200, 200],
centers=[[0, 0], [6, 0], [3, 6]],
cluster_std=[0.3, 1.2, 0.6],
random_state=0,
)
X = StandardScaler().fit_transform(X)
opt = OPTICS(min_samples=10, xi=0.05, min_cluster_size=20).fit(X)
labels = opt.labels_ # -1 marks noise, as in DBSCAN
print(sorted(set(labels)))
print(opt.reachability_[opt.ordering_][:5])The xi parameter sets how steep a drop in the reachability plot must be to count as the edge of a cluster. OPTICS is slower than DBSCAN, and its results depend on xi and min_cluster_size, so you trade one tuning problem for another.
HDBSCAN: a hierarchy of DBSCAN results
HDBSCAN (Hierarchical DBSCAN) effectively runs DBSCAN at every eps and builds a tree from the results. It then keeps the clusters that stay stable over the widest range of density levels. Points that never join a stable cluster are noise.
Its main parameter is min_cluster_size, the smallest group you are willing to call a cluster. This is usually easier to choose than eps. scikit-learn includes HDBSCAN from version 1.3.
from sklearn.cluster import HDBSCAN
from sklearn.datasets import make_blobs
from sklearn.preprocessing import StandardScaler
X, _ = make_blobs(
n_samples=[200, 200, 200],
centers=[[0, 0], [6, 0], [3, 6]],
cluster_std=[0.3, 1.2, 0.6],
random_state=0,
)
X = StandardScaler().fit_transform(X)
hdb = HDBSCAN(min_cluster_size=20).fit(X)
labels = hdb.labels_
print(sorted(set(labels)))Comparing the three
| Method | Density levels | Main parameters | Typical cost |
|---|---|---|---|
| DBSCAN | One | `eps`, `min_samples` | Lowest |
| OPTICS | Many, via reachability plot | `min_samples`, `xi`, `min_cluster_size` | Higher |
| HDBSCAN | Many, via stability | `min_cluster_size`, `min_samples` | Higher |
Common errors
- Treating
-1as a cluster when computing metrics. Exclude noise first, as you did for the silhouette score. - Setting
min_samplestoo low. The result is many small clusters of noise. - Expecting identical output to DBSCAN. HDBSCAN has no
epsand picks its own density levels.
Exercise
A dataset has one tight cluster and one sparse cluster. DBSCAN with a small eps labels most of the sparse cluster as noise, and a larger eps merges the tight cluster with nearby points. Which method would you try next, and why?
Show answer
Try HDBSCAN (or OPTICS). Both evaluate many density levels instead of one global eps, so they can keep the tight cluster and the sparse cluster separate. HDBSCAN is often the easier first choice because its main parameter, minclustersize, is simpler to set than eps or xi.