Intermediate level · Module 6: Topic Modelling and Document Analysis · Lesson 1 of 3
How LDA Discovers Topics
Learners study the idea behind Latent Dirichlet Allocation (documents as mixtures of topics, topics as mixtures of words) and fit a first model.
In this lesson you will
- Explain LDA's generative idea
- Fit an LDA model with scikit-learn
Key concepts
topicdocument-topic distributiontopic-word distributionLatentDirichletAllocationunsupervised learning
The lesson
A topic model finds groups of words that tend to appear together, without labels. In LDA, each document is a mixture of topics (for example 70% education, 30% health) and each topic is a distribution over words.
scikit-learn's LatentDirichletAllocation works on word counts, not TF-IDF: X = CountVectorizer(min_df=2, max_df=0.9, stop_words='english').fit_transform(docs); lda = LatentDirichletAllocation(n_components=8, random_state=0).fit(X). lda.components_ holds topic-word weights and lda.transform(X) gives document-topic mixtures.
Topic models do not understand meaning. They find co-occurrence patterns, which people must interpret and check.
Step by step
- Prepare a count matrix of the project corpus with stop words removed.
- Fit LDA with 8 topics and a fixed random_state.
- Print the top 10 words for each topic.
- Print the main topic of five documents and check whether it makes sense.
Practice activity
Fit a first LDA model on the project corpus and list each topic's top words.
Expected result: A fitted model with top-word lists and a first judgement of topic quality.
Check your understanding
-
Does LDA need labelled data?
Show answer
No. It is unsupervised.
-
Why does scikit-learn's LDA use counts rather than TF-IDF?
Show answer
LDA models word counts generated from topics.
Further learning
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.