Sub topic 12
Clustering with HDBSCAN
This chapter assumes you understand vector embeddings, distances between vectors, and the basic purpose of UMAP. HDBSCAN is the clustering step in the usual BERTopic pipeline. It groups nearby document vectors without requiring you to choose the number of clusters first.
What HDBSCAN does
HDBSCAN, or Hierarchical Density-Based Spatial Clustering of Applications with Noise, finds areas where points are packed closely together. A sparse area between two dense areas becomes a boundary. Points that do not belong confidently to any dense area are marked as noise.
Unlike k-means, HDBSCAN does not need a value such as k=10. It can find clusters of different sizes and can leave unusual documents unassigned.
The algorithm builds a hierarchy of possible clusters at different density levels. It then condenses that hierarchy and selects stable groups. The default excess-of-mass method, often called eom, prefers clusters that persist across a wide range of density levels.
Important parameters
| Parameter | Meaning | Typical effect |
|---|---|---|
| `min_cluster_size` | Smallest allowed cluster | Larger values produce fewer, broader clusters |
| `min_samples` | Strictness of density estimates | Larger values mark more points as noise |
| `metric` | Distance measure | Must match the geometry of your vectors |
In BERTopic, HDBSCAN usually receives UMAP output rather than the original high-dimensional embeddings. UMAP commonly produces five dimensions for clustering. With Euclidean UMAP coordinates, use metric="euclidean". If you cluster the original embeddings directly, cosine distance may be more suitable.
A small working example
Install the required packages with pip install hdbscan scikit-learn. This example creates three groups and detects them:
from hdbscan import HDBSCAN
from sklearn.datasets import make_blobs
vectors, _ = make_blobs(
n_samples=120,
centers=3,
cluster_std=0.7,
random_state=42,
)
clusterer = HDBSCAN(
min_cluster_size=15,
min_samples=5,
metric="euclidean",
cluster_selection_method="eom",
prediction_data=True,
)
labels = clusterer.fit_predict(vectors)
print("Cluster labels:", sorted(set(labels)))
print("Noise documents:", sum(labels == -1))
print("First probabilities:", clusterer.probabilities_[:10])Labels are identifiers, not ordered scores. Label 2 is not “more important” than label 0. The probabilities_ values indicate how strongly points belong to their assigned clusters. BERTopic uses label -1 for outliers.
Common tuning errors
If nearly everything is noise, increase UMAP n_neighbors, reduce min_samples, or check that the distance metric matches the representation. If one giant cluster absorbs unrelated documents, try a smaller UMAP neighborhood or a stricter HDBSCAN configuration. Always inspect representative documents after changing parameters.
Exercise
Why can HDBSCAN return label -1 for a document, and why is that useful in topic modelling?
Show answer
The document is not in a sufficiently dense region to receive a reliable cluster assignment. Keeping it as an outlier avoids forcing an unrelated document into a misleading topic.