Hierarchical clustering and DBSCAN
Where to cut a dendrogram, how to set DBSCAN’s ε, and how both differ from k-means — shown on 40 world cities.
Say "cluster analysis" and most people think of k-means first. But once you try it, a question comes up.
"Do I have to pick the number of clusters up front?"
And what about cases that don't fit anywhere? Two methods worth knowing for exactly this are hierarchical clustering and DBSCAN. Here is how they differ from k-means, and when to use which.
How they differ from k-means
K-means fixes the number of clusters (k) first and groups cases around the nearest centre. (We covered it in Customer segments with k-means.)
- Hierarchical clustering — sets no number; it merges the closest cases step by step into a full family tree, which you then cut at the height you want
- DBSCAN — treats dense crowds of points as clusters and sets aside points that belong nowhere as noise
Today's example
- Cases — 40 cities around the world
- Variables — mean annual temperature, annual temperature range, annual rainfall, annual sunshine hours
The units differ (°C, mm, hours), so we standardize before the analysis. Otherwise a variable with big numbers, like rainfall, would dominate the distances on its own.
What is hierarchical clustering?
At the start, each of the 40 cities is its own cluster. Merge the two closest, then the next two closest… and repeat until everything is one. The tree that draws this merging is the dendrogram.
Reading a dendrogram
- Branches that join low are similar cities
- Branches that join high are different groups
- Draw a horizontal line to cut it, and you get as many clusters as branches below the line
Not sure where to cut? Look for the longest empty vertical stretch. Cutting there splits the tree where the differences are largest. Then check that each cluster can be named and explained.
Which linkage to choose
Linkage decides how the distance between two clusters is measured.
| Linkage | Based on | Character |
|---|---|---|
| Ward | Merge that adds the least within-cluster spread | Even-sized clusters · the most common default |
| Complete | The two farthest points | Compact clusters |
| Average | Mean distance over all pairs | A balanced choice |
| Single | The two closest points | Tends to form long chains |
If unsure, start with Ward linkage + Euclidean distance. Check how faithfully the dendrogram keeps the original distances with the cophenetic correlation (cophenetic r); 0.75 or higher is considered fine.
What is DBSCAN?
DBSCAN finds clusters by asking "Do I have enough neighbours around me?"
- ε (epsilon) — the radius within which points count as neighbours
- minPts (minimum samples) — the minimum number of points needed within that radius
These two values sort points into three kinds.
- Core point — at least minPts neighbours within the radius
- Border point — few neighbours, but close to a core point
- Noise — neither; a loner
So you don't have to fix the number of clusters, and it can find clusters that aren't round.
How to set ε
ε is usually chosen with a k-distance plot. Sort each point's distance to its k-th nearest neighbour from small to large, and at some point the curve bends sharply upward (the knee). Set ε near there.
minPts usually starts at number of variables + 1 or more; with 4 variables as in our example, 4–5 is common. A noise share of about 5–20% generally means a sensible choice. If there is too much noise, try a slightly larger ε.
Splitting with both methods
Hierarchical clustering (Ward, 4 clusters) gives four groups: humid tropical · dry and sunny · continental · mild maritime. But cities like Lima (a desert, yet cloudy) or Reykjavik still get squeezed in somewhere. Hierarchical clustering puts every case into a cluster.
Run DBSCAN on the same data and four cities — Tehran, Harbin, Ulaanbaatar and Lima — drop out as noise. The "cities that don't quite fit anywhere" come to light.
When to use which
| If… | Use |
|---|---|
| You roughly know the number of clusters and they are round blobs | K-means |
| You have tens to hundreds of cases and want to see how they merge | Hierarchical clustering |
| Shapes vary, or you want to filter out odd cases | DBSCAN |
| Density differs a lot between clusters | HDBSCAN (no ε needed) |
Rather than picking just one, checking whether two or three methods give similar results is also a good test.
How to write it up
Four climate characteristics of 40 world cities (mean annual temperature, annual temperature range, annual rainfall and annual sunshine hours) were standardized, and a hierarchical cluster analysis was conducted with Ward linkage and Euclidean distance. Based on the dendrogram and cluster indices, four clusters were derived (silhouette = .454, cophenetic correlation = .761). Applying DBSCAN (ε = 1.1, minPts = 4) to the same data yielded four clusters and four noise points (10.0%; Tehran, Harbin, Ulaanbaatar and Lima).
Example sentence · the numbers are made up
In Skari
In Skari Statistics, open the Clustering menu and choose Hierarchical-based › Hierarchical Clustering (HCA) or Density-based › DBSCAN · HDBSCAN. K-Means, K-Medoids and the Gaussian Mixture Model (GMM) are in the same menu. Each analysis runs Variables → Settings → Validation → Result.
In the settings step of hierarchical clustering you choose the linkage (Ward · complete · average · single), distance metric, number of clusters and scaler. Leave the number of clusters empty and the tree is cut automatically at the longest vertical gap in the dendrogram. Choosing Ward sets the distance to Euclidean.

Run hierarchical clustering on your data
Opens Statistics at Clustering → Hierarchical-based → HCA
The report shows the dendrogram. The dashed line is the height that cuts it into four clusters.

Each cluster's variable means and standard deviations are listed in the original units, so naming the clusters is easy.

You also get cluster quality indices — silhouette, Calinski–Harabasz, Davies–Bouldin, cophenetic correlation and bootstrap stability — plus the number of clusters each criterion recommends.

For DBSCAN you set ε, minimum samples, scaler and distance metric. The report includes the k-distance plot used to choose ε.

It also gives counts of core, border and noise points and a cluster table that includes a noise row.

Reports download as Excel, PDF or Word, and DBSCAN also lets you download Original Data + Cluster, your data with a cluster number on every row.
Wrapping up
Hierarchical clustering shows how cases merge; DBSCAN shows where points crowd and who stands alone. Decide where to cut the dendrogram, or where to set ε for DBSCAN, and the groups hidden in your data become much clearer.
Not sure which clustering method?The Academy's clustering guide helps you match the method to the shape and density of your data →