DBSCAN
DBSCAN (Density-Based Spatial Clustering of Applications with Noise) groups points in high-density neighborhoods into clusters of arbitrary shape while labeling isolated points in sparse regions as noise. Using two parameters—neighborhood radius epsilon and minimum point count minPoints—it classifies points as core, border, or noise without requiring the number of clusters k in advance.
Study Card: DBSCAN Clustering
Direct Answer
DBSCAN (Density-Based Spatial Clustering of Applications with Noise) is an unsupervised density-based clustering algorithm that identifies clusters as contiguous regions of high point density separated by low-density regions. Unlike k-means, DBSCAN does not require specifying the number of clusters ahead of time, naturally discovers non-convex cluster shapes, and explicitly labels sparse outliers as noise (-1). Controlled by two hyperparameters—neighborhood radius (eps) and minimum density threshold (min_samples)—DBSCAN runs in average time with spatial indexing (KD-Tree or Ball Tree) in low dimensions and worst-case without an index or in high dimensions.
Key Terms
- Epsilon ( /
eps): The neighborhood radius around a point : . - MinPoints (
min_samples): The minimum number of points required within (including itself) to form a dense core region. - Core Point: A point satisfying .
- Border Point: A point with that lies within the -neighborhood of at least one core point.
- Noise Point (Outlier): Any point that is neither a core point nor within of any core point.
- Density-Reachability & Density-Connectivity: A point is density-reachable from core point via a chain of core points separated by at most ; maximal sets of density-connected points form clusters.
Example
Consider mapping customer GPS coordinates across a city to place retail pickup lockers. Choosing and flags every location with at least 10 customers within 500 meters as a core point, merges overlapping dense neighborhoods into contiguous urban clusters (even elongated transit corridors that k-means would split), and leaves scattered rural addresses as noise. To select systematically, plot sorted distances to each point’s -th nearest neighbor () and pick the knee where distances jump sharply.
Code Implementation
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN
from sklearn.datasets import make_moons
from sklearn.preprocessing import StandardScaler
# Generate non-convex moons dataset and standardize features
X, _ = make_moons(n_samples=200, noise=0.05, random_state=42)
X = StandardScaler().fit_transform(X)
# Apply DBSCAN (eps = radius, min_samples = minPoints)
dbscan = DBSCAN(eps=0.3, min_samples=5)
clusters = dbscan.fit_predict(X)
n_clusters = len(set(clusters) - {-1})
n_noise = int(np.sum(clusters == -1))
print(f"Estimated clusters: {n_clusters}, Noise points: {n_noise}")
# Visualize clusters and noise (-1 in black)
plt.figure(figsize=(8, 6))
unique_labels = sorted(set(clusters))
colors = [plt.cm.Spectral(each) for each in np.linspace(0, 1, len(unique_labels))]
for k, col in zip(unique_labels, colors):
if k == -1:
col = [0, 0, 0, 1]
xy = X[clusters == k]
label = "Noise" if k == -1 else f"Cluster {k}"
plt.plot(
xy[:, 0], xy[:, 1], 'o',
markerfacecolor=tuple(col), markeredgecolor='k', markersize=8, label=label
)
plt.title('DBSCAN Clustering')
plt.xlabel('Standardized Feature 1')
plt.ylabel('Standardized Feature 2')
plt.legend()
plt.show()
Related Concepts
- DBSCAN vs. K-Means: K-means assumes roughly spherical, equal-variance clusters and assigns outliers to the nearest centroid; DBSCAN handles arbitrary shapes and noise without fixing , though k-means scales better ( per iteration) to very large high-dimensional datasets.
- Varying-Density Limitation & HDBSCAN: Standard DBSCAN uses a single global threshold and struggles when clusters have widely differing densities; HDBSCAN builds a hierarchical density tree to extract stable clusters across varying densities.
- Parameter Selection (-Distance Plot): Choosing (often for -dimensional data) and picking at the elbow of the sorted -nearest-neighbor distance curve gives principled starting hyperparameters.
- Curse of Dimensionality: In high dimensions, pairwise distances become concentrated and spatial trees degrade toward scans; applying PCA or UMAP before DBSCAN often restores meaningful local density separation.