Sub topic 16
Choosing a method for your data
This chapter assumes you know how agglomerative clustering and DBSCAN work, can read a dendrogram and a k-distance plot, and have scaled features before using either method. Here you will turn that knowledge into a decision.
Start from what you know about the data
Neither method wins in general. The better choice depends on four things: the shape of the clusters, whether outliers matter, how much data you have, and whether you need a hierarchy.
| Question | Points toward hierarchical | Points toward DBSCAN |
|---|---|---|
| Do you want a tree of nested groups? | Yes | No |
| Are clusters compact blobs? | Ward or complete linkage fits well | Works, but offers no advantage |
| Are clusters curved or irregular? | Single linkage can work but chains easily | Handles them well |
| Are there outliers to flag? | Outliers get forced into clusters | Labels them -1 |
| Is the dataset large? | Needs at least O(n²) memory for distances | Scales better with a neighbor index |
| Do you know the number of clusters? | Helpful, but you can read it from the tree | Not needed |
| Is density uneven across clusters? | Copes reasonably | One eps fits poorly |
A decision flow
Practical rules
- Small data, need to explore: Use hierarchical clustering. The dendrogram shows structure at every scale, so you can pick the cut later.
- Compact groups of similar size: Ward linkage is a good default.
- Odd shapes plus noise: Use DBSCAN, and choose
epsfrom the k-distance plot. - Thousands to hundreds of thousands of points: Full hierarchical clustering gets expensive, so prefer DBSCAN or a different algorithm.
- Varying density: Try HDBSCAN or OPTICS rather than tuning one
eps.
Compare on your own data
When the choice is unclear, run both and compare. The code below uses the half-moon data from earlier chapters.
from sklearn.cluster import AgglomerativeClustering, DBSCAN
from sklearn.datasets import make_moons
from sklearn.metrics import adjusted_rand_score
from sklearn.preprocessing import StandardScaler
X, y = make_moons(n_samples=300, noise=0.05, random_state=0)
X = StandardScaler().fit_transform(X)
ward = AgglomerativeClustering(n_clusters=2, linkage="ward").fit_predict(X)
single = AgglomerativeClustering(n_clusters=2, linkage="single").fit_predict(X)
dbscan = DBSCAN(eps=0.3, min_samples=5).fit_predict(X)
# The true moon labels are used here only because this is synthetic data.
for name, labels in [("ward", ward), ("single", single), ("dbscan", dbscan)]:
print(name, round(adjusted_rand_score(y, labels), 2))Ward cuts each moon in half because it favors compact groups. Single linkage and DBSCAN follow the curved shapes. On real data you have no true labels, so compare with silhouette scores (excluding noise points) and, more importantly, inspect the clusters yourself.
Common mistakes
- Choosing by silhouette score alone. It rewards convex clusters, so it can rank a worse result above a better one on non-convex data.
- Skipping scaling. Both methods rely on distance, so a feature with a large range dominates.
- Treating DBSCAN noise as a failure. For anomaly detection, it is the useful output.
Exercise
A dataset has 500,000 GPS points forming irregular city-shaped hotspots, plus scattered stray readings. Which method fits better, and why?
Show answer
DBSCAN. The hotspots have irregular shapes, and the stray readings should be labeled as noise. Full hierarchical clustering would need a distance matrix of about 500,000² entries, which is too large to store, and it would force the stray points into clusters.