# BERTopic — Neural Topic Modeling with Transformers > BERTopic is a topic modeling library that uses transformer embeddings, UMAP dimensionality reduction, and HDBSCAN clustering to discover interpretable topics from text documents. ## Install Save as a script file and run: # BERTopic — Neural Topic Modeling with Transformers ## Quick Use ```bash pip install bertopic python -c " from bertopic import BERTopic from sklearn.datasets import fetch_20newsgroups docs = fetch_20newsgroups(subset='all', remove=('headers','footers','quotes'))['data'] topic_model = BERTopic() topics, probs = topic_model.fit_transform(docs) print(topic_model.get_topic_info().head(10)) " ``` ## Introduction BERTopic is a topic modeling technique that combines document embeddings from sentence transformers with a class-based TF-IDF procedure (c-TF-IDF) to generate coherent topic representations. Unlike LDA, it does not assume a fixed number of topics and can discover the optimal topic count automatically through density-based clustering. ## What BERTopic Does - Discovers topics from document collections without specifying the number of topics in advance - Represents each topic with interpretable keywords derived from c-TF-IDF scores - Supports dynamic topic modeling to track how topics evolve over time - Enables hierarchical topic merging for exploring topic granularity - Generates topic visualizations including intertopic distance maps and bar charts ## Architecture Overview BERTopic's pipeline has four stages. First, documents are embedded into dense vectors using a sentence transformer (or any custom embedding model). Second, UMAP reduces the embedding dimensionality while preserving local structure. Third, HDBSCAN clusters the reduced embeddings into groups, with outliers assigned to a noise topic. Fourth, c-TF-IDF computes per-topic word importance scores to produce human-readable topic labels. Each stage is modular and can be swapped with alternatives (PCA instead of UMAP, k-means instead of HDBSCAN, KeyBERT for representation). ## Self-Hosting & Configuration - Install with `pip install bertopic` (includes sentence-transformers, umap-learn, hdbscan) - Configure the embedding model via `BERTopic(embedding_model='all-MiniLM-L6-v2')` or pass a custom model - Adjust UMAP and HDBSCAN parameters for cluster granularity and noise handling - Save and load trained models with `topic_model.save()` and `BERTopic.load()` - Use GPU-accelerated embeddings for faster processing on large corpora ## Key Features - Modular design: swap out embedding, reduction, clustering, and representation components independently - Dynamic topic modeling tracks topic evolution across timestamps - Online learning mode for incremental updates without retraining from scratch - Multi-modal support: combine text with images for visual topic modeling - LLM-enhanced topic labels using GPT or other language models for more descriptive names ## Comparison with Similar Tools - **Gensim LDA** — classic probabilistic topic model requiring a fixed topic count; BERTopic discovers topics automatically with richer representations - **Top2Vec** — similar embedding-based approach; BERTopic adds c-TF-IDF for more interpretable topics and offers more customization - **Scikit-learn NMF/LDA** — lightweight but limited to bag-of-words; BERTopic uses contextual embeddings - **CorEx** — information-theoretic topic model supporting anchored topics; BERTopic is more flexible with modern transformers - **LDA2Vec** — combines LDA with word2vec; less actively maintained than BERTopic ## FAQ **Q: How many documents does BERTopic need to produce good topics?** A: Generally a few hundred documents suffice, but quality improves with thousands. Very small corpora (under 100 docs) may not cluster well. **Q: Can I specify the number of topics?** A: Yes. Set `nr_topics` to reduce topics after fitting, or use `min_topic_size` and HDBSCAN parameters to control granularity during clustering. **Q: Does BERTopic support languages other than English?** A: Yes. Use a multilingual embedding model (e.g., `paraphrase-multilingual-MiniLM-L12-v2`) and BERTopic will discover topics in any language. **Q: Can I update a trained model with new documents?** A: Yes. BERTopic supports online learning via `partial_fit()` and `transform()` to incorporate new documents without full retraining. ## Sources - https://github.com/MaartenGr/BERTopic - https://maartengr.github.io/BERTopic/ --- Source: https://tokrepo.com/en/workflows/asset-56cec9fe Author: Script Depot