← Master Index
Vol. 05 Module 5.3 Lecture

DBSCAN

Unsupervised Learning

How This Lesson Fits the Module

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 eps and min_samples with 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).

TermDefinition
Core pointmin_samples points within radius eps
Border pointWithin eps of a core point but not core itself
NoiseNot reachable from any core point; labeled −1
Density-reachableChain 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.

import numpy as np from sklearn.cluster import DBSCAN from sklearn.preprocessing import StandardScaler from sklearn.neighbors import NearestNeighbors X_scaled = StandardScaler().fit_transform(X) # k-distance plot: distance to k-th neighbor (k = min_samples) k = 5 # often set min_samples = 2 * dimensionality as a starting rule nn = NearestNeighbors(n_neighbors=k) nn.fit(X_scaled) distances = np.sort(nn.kneighbors(X_scaled)[0][:, -1]) db = DBSCAN(eps=0.45, min_samples=5, metric="euclidean") labels = db.fit_predict(X_scaled) n_clusters = len(set(labels)) - (1 if -1 in labels else 0) n_noise = (labels == -1).sum() print(f"Clusters: {n_clusters}, Noise: {n_noise}") df["cluster"] = labels df[df["cluster"] == -1] # flagged anomalies
Engineering Habit — k-Distance Plot for eps

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

ParameterEffect if too smallEffect if too large
epsMany tiny clusters; most points become noiseEverything merges into one cluster
min_samplesMore core points; fragile, fragmented clustersFewer 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)
Critical Mistake — Copying eps Across Datasets

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

MethodCluster countOutliersShape assumption
K-MeansFixed kForced into a clusterSpherical, similar variance
DBSCANDiscoveredLabel −1Density-connected regions
HierarchicalCut dendrogramManual or distance thresholdNested structure visible

Knowledge Check

  1. Short Answer: What label does DBSCAN assign to noise? Answer: −1.
  2. True/False: DBSCAN requires you to specify the number of clusters. Answer: False—you specify density parameters, not k.
  3. Multiple Choice: Best tool to pick eps: (a) elbow on inertia, (b) k-distance plot, (c) accuracy score. Answer: (b).
  4. Short Answer: What makes a point a core point? Answer: At least min_samples neighbors within distance eps.
  5. Short Answer: Why scale before DBSCAN? Answer: eps is a fixed radius; unequal feature scales distort distance.
  6. True/False: DBSCAN can find non-spherical clusters. Answer: True.
  7. Multiple Choice: Border points: (a) are noise, (b) are density-reachable from a core point but not themselves core, (c) define k. Answer: (b).
  8. Short Answer: What happens if eps is too small? Answer: Most points become noise or tiny fragments.
  9. True/False: sklearn DBSCAN has a simple predict for new points like K-Means. Answer: False—standard DBSCAN does not assign new points without extra logic.
  10. 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 eps with k-distance plots; set min_samples with 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.
Trainer’s Guide

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.