Sub topic 3
Choosing DBSCAN Parameters and Clustering Methods
Assume you know basic Python, NumPy, Euclidean distance, and scikit-learn’s fit_predict style. This chapter focuses on DBSCAN, a density-based clustering method.
Choosing `eps` and `min_samples`
eps is the neighborhood radius. min_samples is the minimum number of points required inside that radius for a point to be a core point. DBSCAN expands clusters from core points. Points near a core point can become border points. Points that belong to neither group receive label -1, meaning noise.
Use domain knowledge when possible. Otherwise, plot sorted distances to the min_samples-th nearest neighbor. Choose eps near the curve’s elbow, where distances begin rising quickly.
| Parameter | Larger value usually causes |
|---|---|
| `eps` | Larger, fewer clusters and less noise |
| `min_samples` | Denser required regions and more noise |
Scale features first when their units or ranges differ. Otherwise, a large-range feature can dominate distance.
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)
model = DBSCAN(eps=0.3, min_samples=5)
labels = model.fit_predict(X)
n_clusters = len(set(labels)) - int(-1 in labels)
n_noise = int(np.sum(labels == -1))
print(n_clusters, n_noise)Running and evaluating DBSCAN
DBSCAN does not require the number of clusters in advance and can find curved shapes. It can struggle when clusters have very different densities, when distances lose meaning in high dimensions, or when eps is unsuitable. Noise labels also require careful evaluation.
Without true labels, use internal measures such as silhouette score. Compute it on non-noise points and only when at least two clusters remain. Also inspect cluster sizes, plots, and domain meaning. A high score does not prove that the clusters are useful.
Choosing a method
Choose hierarchical clustering when you need a dendrogram, want to inspect several possible cluster counts, or have a small to medium dataset. Choose DBSCAN when density is meaningful and outliers matter. Use OPTICS when density varies across the dataset. HDBSCAN extends density-based clustering by selecting clusters across density levels and handling variable density more naturally.
Exercise
DBSCAN finds one large cluster and labels almost everything else as noise. What should you check first?
Show answer
Check feature scaling and the eps and min_samples values. Compare eps with a k-distance plot and domain knowledge.