Sub topic 10
Clusters as dense regions
This chapter assumes you know Euclidean distance, basic NumPy, and scikit-learn's fit style API. It also assumes you have seen distance-based clustering such as k-means or the hierarchical methods earlier in this tutorial.
The density view
Hierarchical clustering asks which groups are closest. A density-based method asks a different question: where are the points packed tightly, and where are they sparse?
A cluster, in this view, is a region of high point density separated from other such regions by areas of low density. Points in the low-density areas belong to no cluster. They are noise.
This definition has two practical consequences:
- Clusters can have any shape. A curved band of points is one cluster as long as the density stays high along it.
- The method does not need the number of clusters in advance. The count falls out of how many dense regions exist.
Measuring density with a neighborhood
To make "dense" precise, we need a way to count nearby points. The simplest approach uses a radius called eps (epsilon). The eps-neighborhood of a point is the set of all points within distance eps of it, including the point itself.
A point sits in a dense area if its neighborhood holds at least min_samples points. Two numbers therefore define density: a radius and a count. The next chapters build DBSCAN on this idea.
import numpy as np
from sklearn.datasets import make_moons
from sklearn.neighbors import NearestNeighbors
from sklearn.preprocessing import StandardScaler
X, _ = make_moons(n_samples=300, noise=0.05, random_state=0)
X = StandardScaler().fit_transform(X)
eps = 0.3
nn = NearestNeighbors(radius=eps).fit(X)
neighborhoods = nn.radius_neighbors(X, return_distance=False)
# Each neighborhood includes the point itself.
counts = np.array([len(idx) for idx in neighborhoods])
print("min count:", counts.min())
print("mean count:", round(counts.mean(), 1))
print("points with at least 5 neighbors:", int(np.sum(counts >= 5)))The counts vary from point to point. Points inside a moon have many neighbors within 0.3. Points on the thin edges have fewer. Choosing a threshold on those counts is what separates dense from sparse.
Why the view helps
Consider two interleaved half-moons. K-means would split them with a straight boundary, because it favors compact, round groups. Single linkage might follow the shapes but is easily pulled along thin chains of stray points. A density view follows each moon because the points along it are close to their neighbors, and the gap between the moons is empty.
Limits to keep in mind
The definition uses one global radius and one global count. If a dataset has a tight cluster and a loose cluster, no single eps fits both well. Distance also matters again here: if one feature has a much larger scale, the neighborhood stretches along that feature. Scale the features first, as the scaling chapter explained.
Exercise
A dataset has one tight cluster and one spread-out cluster of similar size. You run a density-based method with a single eps. What problem can occur?
Show answer
A small eps fits the tight cluster but marks many points of the spread-out cluster as noise. A large eps captures the spread-out cluster but may merge the tight cluster with nearby points or other clusters. One global radius cannot match both densities.