DBSCAN & Hierarchical Clustering
K-means' blind spot
K-means partitions space by distance to a centroid, which means every cluster it can represent is, geometrically, a convex region — roughly a blob. That's a strong implicit assumption, and it fails whenever real structure in the data isn't blob-shaped: two concentric rings, a crescent wrapped around a cluster, or a dataset where some points are genuine outliers that don't belong to any group but still get forcibly assigned to the nearest centroid anyway.
DBSCAN and hierarchical clustering both drop the centroid assumption entirely, in different ways. DBSCAN defines clusters by density — regions where points are packed closely together, separated by sparser regions. Hierarchical clustering defines clusters by a merge order — repeatedly combining the closest things, building a full nested structure rather than committing to one fixed partition upfront.
DBSCAN: density over distance to a center
DBSCAN (Density-Based Spatial Clustering of Applications with Noise) needs only two parameters: eps, a radius, and min_samples, a minimum neighbor count. It classifies every point into one of three roles:
Core point — has at least min_samples other points within distance eps of it. Border point — within eps of a core point, but doesn't itself have enough neighbors to be core. Noise point — not within eps of any core point; it doesn't belong to any cluster at all.
A cluster is formed by starting at a core point and recursively absorbing every point reachable through a chain of core points within eps of each other. Because clusters grow by following chains of dense regions rather than measuring distance to a single center, DBSCAN can trace out arbitrarily shaped clusters — crescents, rings, winding shapes — as long as the density within them stays above the threshold. And critically, noise points are an explicit, first-class output, not forced into the nearest cluster the way k-means would.
Note
eps has to roughly match the density scale of the data, and a single eps value struggles when different clusters in the same dataset have very different densities (a dense tight cluster and a sparse spread-out cluster rarely both fit one eps well). Standardizing features and inspecting a k-distance plot (sorted distance to the k-th nearest neighbor) before picking eps is standard practice, much like the elbow method is for k-means' k.Agglomerative hierarchical clustering
Hierarchical clustering takes a different route to the same goal of not assuming a fixed shape or cluster count upfront. The most common variant, agglomerative clustering, starts with every point as its own cluster and repeatedly merges the two closest clusters, one merge at a time, until everything collapses into a single cluster. The entire sequence of merges — recorded as a tree — is the actual output; you choose how many clusters you want afterward by deciding where to cut the tree, rather than specifying it in advance.
The definition of "closest clusters" is controlled by a linkage criterion: single linkage (distance between the closest pair of points across two clusters — good at finding elongated, chain-like clusters, but prone to a 'chaining' effect where unrelated points get linked through a thin bridge), complete linkage (distance between the farthest pair — tends to produce compact, evenly sized clusters), and average or Ward linkage (distance based on cluster means or minimizing within-cluster variance — Ward's method in particular tends to produce balanced clusters similar in spirit to k-means, but without requiring k upfront).
Reading a dendrogram
The merge tree is visualized as a dendrogram: a chart where every point starts as a leaf at the bottom, and each merge is drawn as a horizontal bar connecting two branches at the height of the distance at which they merged. Taller merges mean more dissimilar clusters were forced together.
To get a concrete clustering out of a dendrogram, you draw a horizontal cut line at some height and count how many vertical branches it crosses — that's your number of clusters, and the points under each branch are the members. Cutting lower in the tree gives more, smaller clusters; cutting higher gives fewer, larger ones. This is the real advantage over k-means: you get the entire range of possible groupings in one computation and choose the resolution afterward, rather than rerunning the algorithm for every candidate k.
Compare against the K-Means visualization
Run the K-Means visualization and watch how it always settles on round, Voronoi-shaped regions no matter how the points are arranged. Now imagine the same points arranged as two interleaving crescents or nested rings — K-Means would still draw a straight dividing line through them, splitting each crescent in half. DBSCAN and hierarchical clustering are the tools built specifically for that situation: DBSCAN follows the density of each crescent directly, and hierarchical clustering would group points by nearest-neighbor chains rather than by distance to a shared center.
📊 K-Means Clustering Simulator
InteractiveK-means vs. DBSCAN vs. hierarchical clustering
| Property | K-means | DBSCAN | Hierarchical (agglomerative) |
|---|---|---|---|
| Cluster shape assumption | Convex / spherical | Arbitrary — follows density | Depends on linkage; flexible |
| Need to specify in advance | k (number of clusters) | eps and min_samples | Nothing — choose cut height after |
| Handles outliers/noise | No — forces every point into a cluster | Yes — explicit noise label | Not natively — every point ends up in some cluster |
| Scales to large datasets | Yes — efficient, O(nk) per iteration | Moderate — can be slow without spatial indexing | Poor — naive versions are O(n²) to O(n³) |
| Output | Single flat partition | Single flat partition + noise | Full nested tree (dendrogram), cut as needed |
| Best for | Compact, roughly equal-sized groups at known k | Irregular shapes, unknown k, noisy data | Exploring structure at multiple resolutions, small-to-medium n |
Note
What's next
All three of these algorithms assume you've already picked the right distance metric and scaled your features correctly before clustering — get that step wrong and no clustering algorithm will save you. The lesson on ML Pipelines: Chaining Preprocessing and Models covers how to structure preprocessing and modeling steps together so mistakes like that get caught automatically instead of silently corrupting your results.
I build these systems professionally.
Whether it's a RAG pipeline, analytics migration, or AI workflow — let's talk.