Sub topic 11
DBSCAN: core, border, and noise points
This chapter assumes you know Euclidean distance, can read basic NumPy and scikit-learn code, and have seen the idea of clusters as dense regions from the previous chapter. Here you will learn the exact definitions DBSCAN uses to sort every point into one of three roles, and how those definitions turn into clusters.
The two parameters
DBSCAN has two parameters that define "dense":
epsis a radius. The eps-neighborhood of a point is every point within distanceepsof it, including the point itself.min_samplesis a count. A neighborhood is dense if it holds at leastmin_samplespoints.
scikit-learn counts the point itself toward min_samples. With min_samples=5, a point needs four other points within eps. Some textbooks call this parameter MinPts and count differently, so check the convention before comparing results across libraries.
The neighborhood test uses "distance less than or equal to eps". A point exactly eps away counts.
The three roles
Every point gets exactly one role.
| Role | Definition | Label in `labels_` |
|---|---|---|
| Core point | Its eps-neighborhood has at least `min_samples` points | Cluster id (0, 1, ...) |
| Border point | Not core, but within `eps` of at least one core point | Cluster id |
| Noise point | Not core, and not within `eps` of any core point | -1 |
Core points sit inside dense regions. Border points sit on the edge of a cluster. They belong to it, but they do not have enough neighbors to extend it. Noise points are isolated.
From roles to clusters
Two ideas connect points into clusters.
- Point q is directly density-reachable from p if p is a core point and q is within
epsof p. - Two points are density-connected if a chain of core points links them, each step being directly reachable from the previous one.
A cluster is a maximal set of density-connected points. DBSCAN works like this:
- Pick an unvisited point. If it is not core, mark it as tentatively noise and move on.
- If it is core, start a new cluster and add all points in its neighborhood.
- For every core point added, add its neighborhood too. Keep expanding until no new points join.
- Repeat until every point has been visited.
Because the expansion only continues through core points, a cluster can follow a curved shape such as a half-moon. Border points are added to the cluster but never expand it.
A tentative noise point can later turn into a border point. If a cluster grows and reaches it, it is relabeled. Only points that no cluster reaches stay as noise.
A worked example you can check by hand
Take seven points on a line, with eps=0.6 and min_samples=3.
import numpy as np
from sklearn.cluster import DBSCAN
X = np.array([0.0, 0.5, 1.0, 1.5, 5.0, 5.4, 9.0]).reshape(-1, 1)
db = DBSCAN(eps=0.6, min_samples=3).fit(X)
print("labels:", db.labels_)
print("core indices:", db.core_sample_indices_)The output is:
labels: [ 0 0 0 0 -1 -1 -1]
core indices: [1 2]Work through the neighborhoods, counting each point itself:
- 0.0 has neighbors {0.0, 0.5}, which is 2 points. It is not core.
- 0.5 has {0.0, 0.5, 1.0}, which is 3 points. It is core.
- 1.0 has {0.5, 1.0, 1.5}, which is 3 points. It is core.
- 1.5 has {1.0, 1.5}, which is 2 points. It is not core.
- 5.0 and 5.4 see only each other, so each has 2 points. Neither is core, and no core point is within 0.6, so both are noise.
- 9.0 sees only itself and is noise.
The two core points at 0.5 and 1.0 form the cluster. The points at 0.0 and 1.5 are within eps of a core point, so they are border points in the same cluster. The remaining three points are noise.
Notice that the points at 5.0 and 5.4 are close to each other but still count as noise. Two nearby points are not a dense region when min_samples is 3.
Recovering the roles in scikit-learn
The fitted estimator does not store a role label directly. It stores labels_ and core_sample_indices_, and you derive the roles from those two.
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 300, because every point has exactly one role. The exact counts depend on your scikit-learn version and data, so run it rather than trusting a number from this page.
The attribute components_ holds the coordinates of the core points. That is the data a fitted DBSCAN keeps for reasoning about new points.
Edge cases
Border points can be ambiguous
A border point within eps of core points from two different clusters could belong to either. DBSCAN assigns it to whichever cluster reaches it first, so the result can depend on the row order of your data. Core points and noise points do not have this problem. Their roles are the same under any ordering. If you shuffle rows and see a few points switch clusters, border points are the cause.
Duplicate points
Duplicates each count toward the neighborhood. Ten identical rows make a core point with min_samples=5, even though they carry only one location's worth of information. Remove duplicates, or use the sample_weight argument of fit, if that is not what you want.
Everything is noise, or everything is one cluster
If eps is too small, few points have enough neighbors, and most points are noise. If eps is too large, neighborhoods overlap, and separate groups merge into one cluster. Choosing these values is the subject of a later chapter. For now, treat an all -1 result or a single giant cluster as a signal that the parameters do not match the data's scale.
`min_samples=1`
Every point is its own core point, since it counts itself. Nothing is ever noise, and DBSCAN reduces to connected components of the eps-neighbor graph, which behaves like single-linkage clustering cut at height eps.
No `predict` method
DBSCAN has fit and fit_predict, but no predict. A new point is not clustered by the trained model, because assigning it would mean re-running the density test against the data. A common workaround is to check whether the new point lies within eps of any core point in components_, and to take that core point's label. Treat that as a custom rule, not a built-in feature.
Summary of the model
epsandmin_samplestogether define density at a single, global level.- Core points carry the structure, border points are attached to it, and noise points are what is left.
- Clusters are chains of core points plus their border points.
- Core and noise roles are deterministic. Border assignment can vary with data order.
Exercise
Use eps=1.0 and min_samples=3 on the one-dimensional points [0.0, 0.8, 1.6, 2.4, 3.2, 6.0, 6.5]. Count each point toward its own neighborhood. Which points are core, border, and noise?
Show answer
Neighbors within 1.0 (counting the point itself): 0.0 has {0.0, 0.8}, 2 points, so not core. 0.8 has {0.0, 0.8, 1.6}, 3 points, so core. 1.6 has {0.8, 1.6, 2.4}, so core. 2.4 has {1.6, 2.4, 3.2}, so core. 3.2 has {2.4, 3.2}, 2 points, so not core. 6.0 has {6.0, 6.5}, 2 points, and 6.5 has {6.0, 6.5}, 2 points, so neither is core. Core points are 0.8, 1.6, and 2.4. Border points are 0.0 (within 1.0 of the core point at 0.8) and 3.2 (within 1.0 of the core point at 2.4). The points 6.0 and 6.5 are noise, because no core point lies within 1.0 of them. There is one cluster containing five points, and two noise points.
Exercise
You shuffle the rows of a dataset and re-run DBSCAN with the same eps and min_samples. A few points change cluster, but the set of noise points is identical. What kind of point changed, and why?
Show answer
The changed points are border points. A border point within eps of core points from two different clusters is assigned to whichever cluster reaches it first, which depends on processing order. Core points and noise points are defined only by neighborhood counts and distances, so their roles do not depend on row order.