Sub topic 14
Running the BERTopic Pipeline End to End
Assume you know Python, tokenisation, TF-IDF, and the basic idea of vector embeddings. You should also understand that clustering groups similar items without predefined labels.
BERTopic finds topics in four stages:
- It converts each document into a sentence embedding.
- It reduces the embedding dimensions with UMAP.
- It groups nearby documents with HDBSCAN.
- It describes each group with class-based TF-IDF, or c-TF-IDF.
A BERTopic topic is therefore a cluster of semantically similar documents. This differs from an LDA topic, which is a probability distribution over words.
Fit and inspect a model
Install the main package with pip. BERTopic may install several dependencies, including a sentence-transformers embedding model.
pip install bertopicThe following example uses the 20 Newsgroups dataset. Removing headers, footers, and quotations reduces vocabulary that is unrelated to document meaning.
from bertopic import BERTopic
from sklearn.datasets import fetch_20newsgroups
data = fetch_20newsgroups(
subset="all",
remove=("headers", "footers", "quotes"),
)
documents = data["data"]
topic_model = BERTopic(
language="english",
min_topic_size=15,
)
topics, probabilities = topic_model.fit_transform(documents)
print(topic_model.get_topic_info().head(10))
print(topic_model.get_topic(0))The topic information table includes each topic's identifier, document count, and representative name. Topic words are returned with weights. A word with a larger weight better distinguishes that topic from the others.
The probabilities output depends on the clustering configuration. It can represent a document's membership strength, but it is not always available as a simple probability distribution. Treat it as model output whose meaning depends on the selected HDBSCAN and BERTopic settings.
Replace pipeline components
BERTopic exposes the major stages, so you can replace them independently.
| Component | Main choice |
|---|---|
| Embedding model | Meaning and language coverage |
| UMAP | Dimension reduction and neighbourhood structure |
| HDBSCAN | Cluster size and noise handling |
| Vectorizer | Words and phrases used to describe topics |
For example, this configuration uses bigrams, a smaller embedding space, and explicit clustering settings:
from bertopic import BERTopic
from hdbscan import HDBSCAN
from sentence_transformers import SentenceTransformer
from sklearn.feature_extraction.text import CountVectorizer
from umap import UMAP
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
umap_model = UMAP(
n_neighbors=15,
n_components=5,
min_dist=0.0,
metric="cosine",
random_state=42,
)
hdbscan_model = HDBSCAN(
min_cluster_size=15,
prediction_data=True,
)
vectorizer_model = CountVectorizer(
stop_words="english",
ngram_range=(1, 2),
)
topic_model = BERTopic(
embedding_model=embedding_model,
umap_model=umap_model,
hdbscan_model=hdbscan_model,
vectorizer_model=vectorizer_model,
)
# topics, probabilities = topic_model.fit_transform(documents)Exercise
What does c-TF-IDF do in BERTopic?
Show answer
It selects words and phrases that distinguish one document cluster from the other clusters.