Sub topic 10
Clusters as Dense Regions
This chapter assumes you know Euclidean distance, can write basic Python with NumPy, and have used scikit-learn's fit style API. It also assumes you have seen hierarchical clustering, where clusters come from merging the closest groups. Here the question changes. Instead of asking which groups are close, we ask where the data is crowded.
Why a density view is needed
Centroid methods such as k-means describe a cluster by its center. They work well when clusters are compact and roughly round. Many real datasets break that assumption. Think of GPS pings along two roads, or two interleaved half-moons. A moon has no useful center, and a centroid method will cut straight through both shapes.
Single linkage can follow thin shapes, but it chains through any bridge of points, including noise. Neither approach has a clean way to say "this point belongs to no cluster."
The density view fixes both problems with one idea:
- A cluster is a region where points are packed closely together.
- Regions separated by sparse space are different clusters.
- Points in sparse space that connect to no dense region are noise.
Under this view, cluster shape does not matter. A crescent, a ring, and a blob are all clusters if they are dense and separated from other dense areas by gaps.
Defining density with a neighborhood
Density needs a number, so we measure it locally. For a point p, count how many points fall inside a ball of radius eps around p. That ball is the eps-neighborhood. Compare two points:
- A point deep inside a crowd has many neighbors within
eps. - A point far from everyone has only itself.
A threshold on that count, called min_samples, separates dense places from sparse ones. If the neighborhood of p holds at least min_samples points, we call the area around p dense.
These two numbers define density for the whole dataset:
| Parameter | Meaning | Effect of raising it |
|---|---|---|
| `eps` | Radius of the neighborhood | Neighborhoods hold more points, so clusters merge and fewer points become noise |
| `min_samples` | Points needed in a neighborhood to call it dense | Dense areas are harder to qualify, so more points become noise and small clusters vanish |
The next chapter names the point types that follow from this test: core, border, and noise. For now, keep the principle in mind. Density is a local property, and the two parameters set what "local" and "dense" mean.
A detail that trips people up: the count includes the point itself. In scikit-learn, a point with min_samples=5 needs four other points within eps.
Connecting dense areas into clusters
A single dense spot is not yet a cluster. The density view builds clusters by chaining. If two dense neighborhoods overlap, meaning one dense point lies within eps of another, they belong to the same cluster. Repeating this step spreads the cluster across the whole dense region.
This chaining explains why density clusters can take any shape. The cluster follows the data wherever it is continuously dense. It also explains the main failure mode. A thin bridge of points that is dense enough can join two groups you wanted separate.
Comparing the density view with the linkage view
The density idea resembles single linkage, since both allow chains. The difference is that a density method requires each link in the chain to be backed by enough neighbors. A lone point between two groups is not enough to bridge them.
| Question | Hierarchical (single linkage) | Density-based |
|---|---|---|
| What joins two groups? | One close pair of points | A chain of points that each have enough neighbors |
| What happens to outliers? | They join some cluster eventually | They can stay unassigned as noise |
| Must you give the number of clusters? | Yes, or a cut height | No, it follows from the data |
| What do you tune? | Linkage and cut | `eps` and `min_samples` |
Seeing it on data
Two interleaved half-moons are the standard test. K-means fails here, but a density method separates them.
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_ # the label -1 marks noise
n_clusters = len(set(labels)) - (1 if -1 in labels else 0)
n_noise = int(np.sum(labels == -1))
print(n_clusters, n_noise)The code reports how many clusters were found and how many points were left as noise. Nothing in the call tells DBSCAN to look for two clusters. The count comes from the density structure of the data.
Scaling matters here because eps is a distance. If one feature has a range of 1000 and another a range of 1, the large feature dominates every neighborhood. Scale first, as in the earlier chapter on feature scaling.
Where the density definition has limits
A single eps describes a single density. This causes trouble in three cases.
- Varying density. If one cluster is tight and another is loose, an
epsthat fits the tight one splits the loose one into noise. Anepsthat fits the loose one merges the tight one with its neighbors. - Bridges. A sparse but continuous trail of points between two clusters can fuse them if it meets the density threshold.
- High dimensions. Distances between points become similar as dimensions grow. Neighborhoods then lose meaning, because almost every point is about equally far from every other.
Practical rules of thumb
- Start with
min_samplesnear twice the number of features for larger datasets, and at least 3 for small ones. A larger value suits noisy data. - Scale features before choosing
eps. - Choose
epsfrom the distance to the k-th nearest neighbor, which a later chapter covers with the k-distance plot. - Inspect the fraction of noise. If most points are noise,
epsis too small ormin_samplestoo large. If one giant cluster appears,epsis probably too large.
Exercise
A dataset has one tight cluster and one loose, spread-out cluster. With a single eps, you either lose the loose cluster to noise or merge the tight cluster into its surroundings. Which part of the density definition causes this, and what kind of method would address it?
Show answer
The cause is that one eps and one min_samples define a single density threshold for the whole dataset, so it cannot fit two different densities. A method that handles varying density, such as OPTICS or HDBSCAN (covered at the end of this tutorial), addresses it by considering many eps values instead of fixing one.
Exercise
You run DBSCAN with min_samples=5 and a point has exactly four other points within eps. Is the area around it dense? Explain.
Show answer
Yes. The neighborhood count includes the point itself, so it holds five points, which meets the min_samples=5 threshold.