Guide

k-means wants a k and spherical blobs. Two other clusterers cover the cases it misses. DBSCAN finds arbitrarily shaped dense regions and marks sparse points as noise. Hierarchical clustering builds a tree of merges so you can choose k after looking at the structure.

Concept

DBSCAN grows clusters from dense neighborhoods. A core point has at least min_samples neighbors within radius eps. Points in sparse regions are labeled noise (-1). You do not prechoose k.

The big idea

Agglomerative clustering starts with each point alone and merges closest clusters until one tree (dendrogram) remains. Cut the tree at a height to choose k after seeing the structure. Good for small n and interpretability. O(n²) memory and time limit scale.

DBSCAN is very sensitive to eps and to feature scaling. Use it when you care about arbitrarily shaped dense regions and explicit noise points, and you can tune eps / min_samples. Use k-means when you want every point assigned, roughly blob-shaped groups, and a fixed k for capacity planning.

A picture: k-means is drawing k circles and fighting over the borders. DBSCAN is spilling ink on dense puddles and leaving the dry floor as noise. Hierarchical clustering is a family tree of those puddles. The picture breaks at large n, where the tree will not fit in memory.

Walking through DBSCAN

Python
from sklearn.cluster import DBSCAN
from sklearn.datasets import make_blobs
from sklearn.preprocessing import StandardScaler

X, _ = make_blobs(n_samples=600, centers=4, cluster_std=0.8, random_state=0)
X = StandardScaler().fit_transform(X)

labels = DBSCAN(eps=0.4, min_samples=5).fit_predict(X)
n_clusters = len(set(labels) - {-1})
n_noise = int((labels == -1).sum())
print("clusters", n_clusters, "noise", n_noise)

Typical output on well-separated blobs: about 4 clusters and a handful of noise points. Tighten eps and more points become noise. Widen it and clusters merge.

Evaluating without labels:

  • Internal metrics: silhouette, Davies–Bouldin, inertia — weak proxies, usable for relative comparisons.
  • Stability: rerun with different seeds or subsamples. Unstable clusters are not product segments.
  • Downstream: does a sales play per cluster lift conversion?
  • Human rating: sample 50 items per cluster. Measure purity.

You propose k=12 user segments. Silhouette prefers k=6. Sales can only run four plays. You cut the dendrogram, or merge k-means centers, to four actionable segments, name them with exemplar behaviors, and A/B a play on the "power user fading" segment. Lift on retention is the metric. Silhouette was only a compass.

Unsupervised work is successful when it changes a decision, not when silhouette ticks up 0.02.

Common mistakes

  • Treating DBSCAN noise as a bug. Noise is the point: sparse points are not a cluster.
  • Skipping scaling before eps. eps=0.4 is meaningless on mixed-unit columns.
  • Running agglomerative clustering on 2 million rows. O(n²) will not finish kindly.

Check yourself

  1. What does DBSCAN label -1 mean? Answer: Noise — the point was not dense enough to join a cluster.
  2. When would you cut a dendrogram instead of picking k up front? Answer: When you want to see merge structure first, then choose a cut that matches the number of actions you can take.

Key takeaways

  • Hierarchical clustering builds a tree of merges. Cut it after you see the structure.
  • DBSCAN finds dense shapes and explicit noise. It is sensitive to eps and scaling.
  • Internal metrics are a compass. Downstream decisions are the test.
  • Scale features before any distance-based method.

Further learning

Next: PCA for ML.

scikit-learn: ClusteringClustering of unlabeled data can be performed with the module sklearn.cluster. Each clustering algorithm comes in two variants: a class, that implements the fit method to learn the clusters on trai...scikit-learn