Sub topic 11
DBSCAN: core, border, and noise points
This chapter assumes you know Euclidean distance, basic NumPy, and scikit-learn's fit style API. DBSCAN (Density-Based Spatial Clustering of Applications with Noise) groups points by local density. It sorts every point into one of three roles, and those roles determine the clusters.
Two parameters define density
epsis the neighborhood radius. Two points are neighbors if their distance is at mosteps.min_samplesis the number of points, counting the point itself, that a neighborhood must contain for the region to count as dense.
The neighborhood of a point is every point within eps of it, including itself. This detail matters when you compare your own counts with scikit-learn's output.
The three roles
- Core point: its neighborhood has at least
min_samplespoints. - Border point: it is not a core point, but it lies within
epsof at least one core point. - Noise point: it is neither. It gets the label
-1.
How clusters grow
- Pick an unvisited core point and start a new cluster with it.
- Add every point in its neighborhood to the cluster.
- For each newly added core point, add its neighbors too. Repeat until no more points can be added.
- Border points join the cluster but do not extend it, because they are not dense enough to pass the cluster on.
Two core points belong to the same cluster if a chain of core points links them, where each step is at most eps. This chaining is why DBSCAN can follow curved shapes such as half-moons.
A border point can lie within eps of core points from two different clusters. It is assigned to whichever cluster reaches it first, so border assignments can depend on the order of the data. Core and noise labels do not have this problem.
Recovering the roles in scikit-learn
scikit-learn exposes labels_ and core_sample_indices_. Border points are the clustered points that are not core.
import numpy as np
from sklearn.cluster import DBSCAN
from sklearn.datasets import make_moons
from sklearn.preprocessing import StandardScaler
X, _ = make_moons(n_samples=300, noise=0.05, random_state=0)
X = StandardScaler().fit_transform(X)
db = DBSCAN(eps=0.3, min_samples=5).fit(X)
labels = db.labels_
is_core = np.zeros(len(X), dtype=bool)
is_core[db.core_sample_indices_] = True
is_noise = labels == -1
is_border = ~is_core & ~is_noise
print("core:", int(is_core.sum()))
print("border:", int(is_border.sum()))
print("noise:", int(is_noise.sum()))The three counts add up to the number of samples.
Common errors
- Treating noise as a cluster. The label
-1means "no cluster". Exclude it when you count clusters or compute metrics. - Forgetting that a point counts itself. With
min_samples=5, a core point needs only four other neighbors. - Skipping feature scaling.
epsis a single distance for all features, so a feature with a large range dominates the neighborhoods. - Expecting
predict. DBSCAN has nopredictmethod for new points. Usefit_predicton the full dataset.
Exercise
A point has 3 points within eps, not counting itself. min_samples is 4. A core point lies within eps of it. What is its role, and why?
Show answer
Its neighborhood contains 4 points, counting itself, which meets minsamples, so it is a core point. The nearby core point does not change this. If minsamples were 6, it would be a border point instead, because it would fail the density test but sit within eps of a core point.