Sub topic 7
Training and Reading an LDA Model with Gensim
Assume you know Python, tokenisation, stop words, TF-IDF, and basic probability. This chapter uses a bag-of-words corpus: each document is represented by word counts, not word order.
Latent Dirichlet Allocation (LDA) represents each document as a mixture of topics. Each topic is a probability distribution over words. For example, a document might contain 80% of a sports topic and 20% of a politics topic. The topic names do not come from the model. You infer them from their highest-probability words.
Build and train the model
Gensim needs two related inputs:
- A dictionary maps each word to an integer ID.
- A corpus stores each document as
(word_id, count)pairs.
The num_topics argument is a modelling choice. Too few topics combine unrelated themes. Too many topics split one theme into small, noisy groups. Train several candidate models and compare their coherence and interpretability.
from gensim import corpora
from gensim.models import LdaModel, CoherenceModel
# Each document is already tokenised, lower-cased, and stripped of stop words.
docs = [
["goal", "match", "player", "team", "season"],
["election", "vote", "party", "government", "policy"],
["team", "coach", "league", "season", "win"],
["minister", "government", "vote", "law", "policy"],
]
dictionary = corpora.Dictionary(docs)
corpus = [dictionary.doc2bow(doc) for doc in docs]
lda = LdaModel(
corpus=corpus,
id2word=dictionary,
num_topics=2,
passes=10,
random_state=42,
)
for topic_id, words in lda.print_topics(num_words=5):
print(topic_id, words)
coherence = CoherenceModel(
model=lda, texts=docs, dictionary=dictionary, coherence="c_v"
).get_coherence()
print("c_v coherence:", coherence)passes controls how many complete passes the training algorithm makes over the corpus. More passes can improve a small model, but they increase runtime and may not solve poor preprocessing or a bad topic count. random_state makes this run repeatable.
A topic is printed as weighted words, such as 0.24*"team" + 0.18*"season". These weights describe the topic-word distribution. They are not percentages of documents.
Inspect document mixtures
A document can belong to multiple topics. Use get_document_topics to inspect its document-topic distribution. Setting minimum_probability=0.0 includes every topic, including topics with small probabilities.
document_topics = lda.get_document_topics(
corpus[0],
minimum_probability=0.0,
)
print(document_topics)The returned pairs have the form (topic_id, probability). The probabilities for one document should sum to approximately 1. A document containing team, match, and season should receive a higher probability for the sports-like topic than for the politics-like topic.
Exercise
Exercise
Why should you inspect example documents instead of naming a topic from its top words alone?
Show answer
Top words can be ambiguous or shared across topics. Example documents show how the words are used together and help reveal whether the topic is coherent.