Sub topic 11
Reducing embeddings with UMAP
Assume you understand tokenisation, TF-IDF, and the basic idea of a vector embedding. You also need Python and pip. This chapter focuses on how UMAP prepares document embeddings for clustering in a BERTopic-style pipeline.
Why reduce embeddings?
A sentence embedding may contain hundreds of dimensions. Each dimension captures part of a document's meaning, but clustering directly in this space can be expensive and noisy.
UMAP, or Uniform Manifold Approximation and Projection, reduces a high-dimensional matrix to fewer dimensions while trying to preserve nearby points. Documents with similar meanings should remain close after reduction.
UMAP does not assign topic labels. It creates a new representation that a later algorithm, such as HDBSCAN, can cluster.
The main UMAP settings
| Setting | Meaning | Common effect |
|---|---|---|
| `n_neighbors` | Number of nearby points used to build the local structure | Smaller values preserve local groups |
| `n_components` | Number of output dimensions | Use 2 for plotting or more for clustering |
| `min_dist` | Minimum spacing between points in the reduced space | Smaller values make compact groups |
| `metric` | Distance measure for input embeddings | `cosine` is common for text embeddings |
The parameters express a trade-off. A small n_neighbors can reveal narrow, local themes but may split one broad theme into several groups. A large value preserves more global structure but can blur small groups. A two-dimensional result is useful for a chart, but five or more dimensions may work better as input to HDBSCAN.
A small working example
Install the required packages first:
pip install numpy umap-learnThe following example creates artificial embeddings with three groups. In practice, replace embeddings with vectors produced by a sentence-embedding model.
import numpy as np
from umap import UMAP
rng = np.random.default_rng(42)
# Simulated embeddings: 90 documents with 50 features each.
centres = np.array([
np.zeros(50),
np.r_[np.ones(10) * 4, np.zeros(40)],
np.r_[np.zeros(20), np.ones(10) * -4, np.zeros(20)],
])
embeddings = np.vstack([
centre + rng.normal(0, 0.4, size=(30, 50))
for centre in centres
])
reducer = UMAP(
n_neighbors=15,
n_components=5,
min_dist=0.0,
metric="cosine",
random_state=42,
)
reduced_embeddings = reducer.fit_transform(embeddings)
print("Original shape:", embeddings.shape)
print("Reduced shape:", reduced_embeddings.shape)fit_transform learns the reduction from the supplied documents and returns their new coordinates. For new documents, call reducer.transform(new_embeddings) rather than fitting a second reducer. This keeps the coordinate system consistent.
Common errors
- Using
n_components=2automatically for clustering. Two dimensions are easy to plot, but they may discard useful information. - Treating a UMAP plot as proof that topics exist. The plot depends on UMAP settings.
- Fitting UMAP separately for training and test documents. Use
transformfor later data. - Choosing
n_neighborswithout checking corpus size. Very large values leave little local structure in a small corpus.
Exercise
You have 1,000 document embeddings and want both a visual diagnostic and HDBSCAN clustering. Which UMAP setup is a reasonable starting point, and why?
Show answer
Fit one reducer with n_components=2 for the visual diagnostic and another with n_components=5 or more for clustering. The first is easy to plot; the second preserves more information for HDBSCAN.