Skip to content
Coding Script or NotebookGoogle ColabFree

Topic Modelling with LDA in Python: Tuned, Visualised and Linked (Notebook)

ForContent analysisInternal linking

By Lazarina Stoy· Updated 5 Dec 2026

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

  1. 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.
  2. Set USE_DEMO = False and load your crawl export with URL, body text and title.
  3. Clean the text: tokens, bigrams, custom stopwords and filtering of words that appear almost everywhere.
  4. Train the model, then tune the number of topics and the priors using held-out perplexity and c_v coherence.
  5. Explore the interactive pyLDAvis map and the topics per page.
  6. 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