K-Means assumes spherical, evenly sized groups. DBSCAN (Density-Based Spatial Clustering of Applications with Noise) finds clusters as dense regions separated by sparse space—and explicitly labels outliers as noise.
Use DBSCAN when shapes are irregular, cluster count is unknown, or you need a “no cluster” category for anomalies. It is the go-to density method before trying hierarchical approaches.
Learning Objectives
By the end of this lesson, students should be able to:
- Define core points, border points, and noise in DBSCAN.
- Tune
epsandmin_sampleswith domain and k-distance plots. - Implement DBSCAN in sklearn and interpret label −1 as noise.
- Compare DBSCAN to K-Means on non-spherical data.
- Explain why feature scaling strongly affects
eps. - Choose DBSCAN when outlier detection and arbitrary shapes matter.
Core Concepts
DBSCAN grows clusters from core points—observations with at least min_samples neighbors within distance eps. Border points sit within eps of a core point but lack enough neighbors themselves. Everything else is noise (label −1).
| Term | Definition |
|---|---|
| Core point | ≥ min_samples points within radius eps |
| Border point | Within eps of a core point but not core itself |
| Noise | Not reachable from any core point; labeled −1 |
| Density-reachable | Chain of core points within eps connects two observations |
sklearn Implementation
DBSCAN does not have a predict method for new points in older sklearn versions—it is transductive (fits and labels the given set). Scale features so eps is interpretable in standardized space.
Plot sorted distances to the k-th nearest neighbor. The “knee” where the curve bends upward suggests a reasonable eps. Combine with silhouette on non-noise points and business review of the noise fraction.
Tuning eps and min_samples
| Parameter | Effect if too small | Effect if too large |
|---|---|---|
eps | Many tiny clusters; most points become noise | Everything merges into one cluster |
min_samples | More core points; fragile, fragmented clusters | Fewer cores; stricter density requirement |
When DBSCAN Shines
- Arbitrary cluster shapes (rings, blobs)
- Unknown number of clusters
- Built-in noise / anomaly labels
- Geospatial or embedding neighborhoods
When to Avoid DBSCAN
- Clusters differ greatly in density
- Very high dimensions without reduction
- You need fast scoring on streaming new points
- Uniform global density (K-Means may suffice)
eps is not portable. After changing features, scaling, or sample size, re-tune from a k-distance plot. An eps that worked on 10k standardized rows may fail on 1M raw features.
DBSCAN vs K-Means vs Hierarchical
| Method | Cluster count | Outliers | Shape assumption |
|---|---|---|---|
| K-Means | Fixed k | Forced into a cluster | Spherical, similar variance |
| DBSCAN | Discovered | Label −1 | Density-connected regions |
| Hierarchical | Cut dendrogram | Manual or distance threshold | Nested structure visible |
Knowledge Check
- Short Answer: What label does DBSCAN assign to noise? Answer: −1.
- True/False: DBSCAN requires you to specify the number of clusters. Answer: False—you specify density parameters, not k.
- Multiple Choice: Best tool to pick eps: (a) elbow on inertia, (b) k-distance plot, (c) accuracy score. Answer: (b).
- Short Answer: What makes a point a core point? Answer: At least min_samples neighbors within distance eps.
- Short Answer: Why scale before DBSCAN? Answer: eps is a fixed radius; unequal feature scales distort distance.
- True/False: DBSCAN can find non-spherical clusters. Answer: True.
- Multiple Choice: Border points: (a) are noise, (b) are density-reachable from a core point but not themselves core, (c) define k. Answer: (b).
- Short Answer: What happens if
epsis too small? Answer: Most points become noise or tiny fragments. - True/False: sklearn DBSCAN has a simple
predictfor new points like K-Means. Answer: False—standard DBSCAN does not assign new points without extra logic. - Multiple Choice: High-dimensional data makes
eps: (a) easier, (b) harder due to distance concentration, (c) irrelevant. Answer: (b).
Key Takeaways
- DBSCAN finds density-connected clusters and flags sparse points as noise.
- Tune
epswith k-distance plots; setmin_sampleswith dimensionality in mind. - Scale features; re-tune when the feature space changes.
- Prefer K-Means for fixed, spherical segments; DBSCAN for irregular shapes and anomalies.
- Next: Hierarchical Clustering—build and cut cluster trees.
Hands-on idea: Use sklearn’s make_moons dataset. Show K-Means failure vs DBSCAN success, then sweep eps to demonstrate over-merging and over-fragmentation.
Discussion prompt: Fraud analysts want noise points reviewed daily. What min_samples trade-off balances false alarms vs missed fraud?
Recap: DBSCAN finds density-connected clusters and labels sparse points as noise without choosing k. Continue with Hierarchical Clustering.