Sub topic 1
What topic modelling does and when to use it
Prerequisites
This chapter assumes you can write basic Python and install packages with pip. You should know simple probability, including distributions, sampling, and conditional probability. You should also know tokenisation, stop words, and TF-IDF. No model needs to be trained yet.
What topic modelling does
Topic modelling is an unsupervised method that finds recurring themes in a collection of documents. It needs no labels. It returns a set of topics, where each topic is a weighted list of words, and each document is a mix of those topics. Use it to skim large collections such as support tickets, survey answers, or news archives, or when you do not know the themes in advance.
It is a poor fit when you need fixed categories or when documents are very short, because short texts give the model too few words to work with.
Preparing text for topic models
Lower-case the text, split it into tokens, and remove stop words such as "the" and "and". Drop very rare and very common words. Lemmatise or stem tokens so that "running" and "runs" count as one term. Preprocessing choices shape the topics, so record them with your results.
Bag-of-words and document-term counts
A bag-of-words representation ignores word order. Each document becomes a vector of counts, with one column per vocabulary word. Stacking these vectors for all documents gives the document-term matrix. Grammar and word order are lost, but counts are enough for many topic models.
Dirichlet priors: alpha and eta
A Dirichlet distribution produces a probability vector, a set of non-negative values that sum to one. Two Dirichlet priors control LDA. Alpha sets how many topics a document tends to mix. Small alpha favours documents dominated by a few topics. Eta sets how many words a topic tends to use. Small eta favours topics built from a few distinctive words.
How LDA imagines documents being written
LDA assumes a generative story. For each topic, draw a word distribution from a Dirichlet with prior eta. For each document, draw a topic mixture from a Dirichlet with prior alpha. For each word slot, pick a topic from the document's mixture, then pick a word from that topic's distribution. Training runs this story in reverse: it infers the hidden topics from the observed words.
Fitting LDA: variational Bayes and Gibbs sampling
Exact inference is intractable, so two approximations are common. Variational Bayes replaces the true posterior with a simpler distribution and optimises that. Gensim uses it by default. It is fast, and it is repeatable when you fix the random seed. Gibbs sampling repeatedly resamples each word's topic given all other assignments. It is slower but converges to the exact posterior in the limit, and research code often uses it.
Exercise
A document-term matrix has 1,000 documents and 5,000 vocabulary words. What are its dimensions, and why are most of its entries zero?
Show answer
It is 1,000 by 5,000. Most entries are zero because each document uses only a small fraction of the vocabulary.