liveBook · Manning

Latent Dirichlet allocation

As explained in Natural Language Processing in Action, Second Edition

Latent Dirichlet allocation is a probabilistic topic-modeling technique that represents documents as mixtures of latent topics and topics as distributions over words.

Keyword counts can miss a document’s subject when related words do not match exactly. Latent Dirichlet allocation (LDiA) turns bag-of-words counts into topic vectors: compact representations based on which words tend to occur together. These vectors can support semantic search, topic analysis, and downstream classifiers. section 4.0 section 4.11

Think of a document as a mixture of topics and each topic as a distribution over words. LDiA starts with a chosen number of topics and uses Dirichlet probability distributions as priors for topic weights and word assignments. An imagined document generator would choose a length, mix topics, and draw words appropriate to them; the model works backward from observed word counts to estimate the mixtures. It models bags of words, not word order. section 4.5 section 4.5.1

Inspect topics in comments

For a toxic-comments dataset, make raw count vectors with `CountVectorizer(tokenizer=spacy_tokenize)`, then fit `LatentDirichletAllocation(n_components=16, learning_method='batch')`. Inspect `components_` to see how terms are allocated across topics; compare words such as “hate,” “stupid,” and “idiot.” Call `transform(bow_docs)` to obtain one topic vector per comment. The model allocates 19,169 terms across 16 topics in this example, but individual allocations can change between runs because training is stochastic; fix a random seed when reproducibility matters. section 4.5.2

>>> from sklearn.decomposition import LatentDirichletAllocation as LDiA

>>> ldia = LDiA(n_components=16, learning_method='batch')
>>> ldia = ldia.fit(bow_docs)     #1
>>> ldia.components_.shape
(16, 19169)

The fitting snippet shows the scikit-learn estimator, chosen topic count, raw-count input, and shape of its topic-by-term output. section 4.5.2

More interpretable topics do not guarantee a better classifier. A linear discriminant analysis classifier trained on 16-topic LDiA vectors performs worse on the toxicity task than one trained on 16-topic latent semantic analysis (LSA) vectors. Raising LDiA to 32 topics improves precision, recall, and F1 in this dataset, but still does not match LSA. Here, LDA names the classifier; LDiA names latent Dirichlet allocation. section 4.5.3 section 4.5.4

Try LSA first for most topic modeling, semantic search, recommendation, classification, or regression work: its linear approach is faster and often more practical. Choose LDiA when explaining the words in each topic matters, including topic analysis and some summarization tasks. Check downstream performance rather than assuming human-readable topics will predict better. section 4.1.4 section 4.5 section 4.5.4

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains document–topic mixtures and Dirichlet priors, then fits and evaluates a scikit-learn model without deriving its statistical algorithm.
Examples
Fits 16-topic and 32-topic models to toxic comments, inspects term allocations, and compares classifier performance.
What it assumes
Assumes familiarity with bag-of-words and TF–IDF vectors; explains topic mixtures and priors through a document-generation analogy.
What you can do after
Apply it via scikit-learn to build comment topic vectors, inspect their terms, and evaluate them as classifier features.
How it connects
Connects bag-of-words vectors, topic vectors, latent semantic analysis, semantic search, and linear discriminant analysis.

Also explained in

Compare how 2 books explain latent Dirichlet allocation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.