Topic Modelling with LDA in Python: Tuned, Visualised and Linked (Notebook)
Latent Dirichlet Allocation (LDA) is still one of the clearest ways to see which topics a site actually covers, as long as you clean the text well and check that the topics hold together. This free Google Colab notebook is the 2026 refresh of my LDA workflow on current versions of gensim and pyLDAvis, from a crawl export to page-level topics and internal link candidates.
Who it’s for
SEOs and content strategists who want to audit topical coverage across their own site or a competitor’s, and anyone who followed an older LDA tutorial and hit errors from renamed gensim and pyLDAvis functions. No API keys are needed.
How to use it
- Open the notebook in Colab, uncomment the install line in the first code cell, and run it on the demo data first: about 3,400 posts from six groups of the public 20 Newsgroups dataset.
- Set
USE_DEMO = Falseand load your crawl export with URL, body text and title. - Clean the text: tokens, bigrams, custom stopwords and filtering of words that appear almost everywhere.
- Train the model, then tune the number of topics and the priors using held-out perplexity and c_v coherence.
- Explore the interactive pyLDAvis map and the topics per page.
- Review the internal link candidates and export.
What you get
- Soft topic membership for every page, its dominant topic and the top pages per topic
- A tuning table with coherence and correctly computed perplexity
- An interactive pyLDAvis inter-topic map saved as HTML
- A topic correlation heatmap and the top five internal link candidates per page, from the similarity of their topic mixes
- An NMF comparison and a titles-only run, plus CSV exports
Why tuning matters
The number of topics is the decision that changes the results most. Pick too few and unrelated pages share a topic; pick too many and one theme splits into near-duplicates. Coherence scores give you an evidence-based shortlist, and pyLDAvis lets you check by eye that the topics are distinct before you name them.
Limits
LDA struggles on short text. On the demo data, c_v coherence fell from about 0.67 on full text to 0.37 on titles alone, which is why the notebook includes the short-text comparison. Topic models also need a person to name and sanity-check topics before they drive decisions, and the link candidates are a review list before anything goes live.
The full write-up is Topic Modelling for SEO with LDA (Latent Dirichlet Allocation). A guided course edition of this lab, with demo data, a no-code output organiser and a video walkthrough, is part of Introduction to Machine Learning for SEO.
What’s included
- From a crawl export to topics per page, on current gensim and pyLDAvis
- Tuning with held-out perplexity (computed correctly) and c_v coherence
- Interactive pyLDAvis map saved as HTML
- Topic correlation heatmap and top five internal link candidates per page
- NMF comparison and a titles-only run that shows the short-text problem; no API keys
Where this resource is used on MLforSEO
Walkthroughs on the blog
Concepts behind it
More content analysis resources
- Coding Script or NotebookTopic Modelling with BERTopic in Python: Pages and Short Text (Notebook)
- Coding Script or NotebookWhisper API Transcription Notebook: Bulk Audio, Long Files and SRT Captions
- Coding Script or NotebookScreaming Frog Custom Extraction Helper: Selector Tester and Dataset Cleaner
- GuideEntity Extraction Benchmark: Generative AI vs NLP APIs (Raw Results Sheet)
- GuideContent Transformation Approaches in ML: 16 Types Compared (Guide)
- No-Code TemplateNo-code LDA Topic Modeling: Website Content Clustering Output Organiser (Google Sheets Template)
See all 29 content analysis resources →See all 26 internal linking resources →See all 73 coding script or notebook resources →
