Sub topic 12
Choosing eps and min_samples
This chapter assumes you know how DBSCAN labels core, border, and noise points, and that you can use scikit-learn's fit API. It also assumes you have scaled your features, because both parameters depend on distance.
What the two parameters control
eps is the radius of the neighborhood around each point. min_samples is the number of points, counting the point itself, that must fall inside that radius for a point to be a core point. Together they define "dense enough".
| Change | Effect on clusters | Effect on noise |
|---|---|---|
| `eps` too small | Clusters split into fragments | Many points become noise |
| `eps` too large | Separate clusters merge into one | Few or no noise points |
| `min_samples` too small | Sparse groups and chains of outliers count as clusters | Little noise |
| `min_samples` too large | Only very dense regions survive | More noise |
Start with min_samples
Choose min_samples first, because the eps heuristic depends on it. A common rule of thumb is at least the number of features plus one, and often 2 * n_features. For 2-D data, 4 or 5 is a usual start. Use larger values for noisy data or large datasets, since a bigger value makes the density test stricter.
Find eps with the k-distance plot
For each point, compute the distance to its k-th nearest neighbor, where k equals min_samples. Sort these distances and plot them. Points inside clusters have small k-distances. Noise points have large ones. The curve rises slowly and then bends sharply upward, and the distance at that bend is a candidate for eps.
import numpy as np
import matplotlib.pyplot as plt
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)
min_samples = 5
# The query points are the training points, so each point is its own first neighbor,
# which matches how DBSCAN counts min_samples.
nn = NearestNeighbors(n_neighbors=min_samples).fit(X)
distances, _ = nn.kneighbors(X)
k_dist = np.sort(distances[:, -1])
plt.plot(k_dist)
plt.xlabel("points sorted by distance")
plt.ylabel(f"distance to neighbor number {min_samples}")
plt.show()Because each point is its own first neighbor, the last column of distances is the distance to the point's min_samples-th member, which is the quantity DBSCAN tests.
Treat the elbow as a starting point
The elbow is a heuristic, not a rule. Real curves are often smooth, and the elbow may span a range of values. Try a few eps values around it and check three things:
- The number of clusters found.
- The fraction of points labeled
-1. - Whether the clusters make sense for your domain.
If 60% of the data is noise, eps is probably too small. If everything is one cluster, it is probably too large. Domain knowledge helps too. If you know two sensor readings within 0.5 units are the same event, that sets a natural scale for eps.
Exercise
A k-distance plot with min_samples=5 shows a clear elbow at 0.4. You run DBSCAN with eps=0.4, and 70% of points are labeled noise. What is a reasonable next step?
Show answer
Raise eps somewhat above 0.4, or lower min_samples, and rerun. Both changes make the density test easier to pass, so more points become core or border points. Check that the clusters stay separate as you do. The elbow is only a starting estimate.