Topic Modeling with LDA in Python
Apply LDA topic modeling to a news article dataset, extract coherent topics, and visualize topic-word distributions.
What this AI workflow does
This AI Data Analyst workflow loads the 20 Newsgroups training split from scikit-learn and converts the text into a TF-IDF document-term matrix while reporting the vocabulary size. It fits a 5-topic Latent Dirichlet Allocation (LDA) model and prints the top words for each topic to support interpretation. It visualizes topic prevalence across the corpus and identifies the dominant topic for a small sample of documents.
Who this example is for
This is for data analysts and NLP practitioners who want a reproducible, code-generating example of LDA topic modeling on a standard benchmark dataset. It helps users validate preprocessing choices, inspect topic-word distributions, and connect topics back to representative documents.
Expected analysis outcomes
These are the results the AI workflow is expected to generate.
- TF-IDF vocabulary size report after filtering
- Top 10 words per topic for 5 LDA topics
- Bar chart of topic prevalence across the dataset
- Table of 5 sample documents with dominant topic labels
Tools and libraries used
Main Python packages and tooling used to run this AI data analysis task.
- scikit-learn
- pandas
- numpy
- matplotlib
- seaborn
Prompt sequence
This is the exact list of prompts used in this workflow. The same prompt sequence is sent to each model so outputs and scores can be compared fairly.
- 1load 20 newsgroups data (20 Newsgroups from sklearn), vectorize with TF-IDF, and show vocabulary size
- 2fit an LDA model with 5 topics and print top 10 words per topic
- 3plot topic distribution across the dataset
- 4show which topic dominates for a sample of 5 documents
Model Comparison
We compared several LLM models on the same analysis task. The table below shows their scores side by side. You can also open each model run to check the full conversation and notebook results.
| Model Source | Score | Task | Execution | Output | Reasoning | Reliability | Run |
|---|---|---|---|---|---|---|---|
| gpt-oss:120b | 10/10 | 2/2 | 2/2 | 3/3 | 2/2 | 1/1 | Open gpt-oss:120b conversation |
| qwen3.5:397b | 10/10 | 2/2 | 2/2 | 3/3 | 2/2 | 1/1 | Open qwen3.5:397b conversation |
| gemma4:31b | 9/10 | 2/2 | 2/2 | 3/3 | 1/2 | 1/1 | Open gemma4:31b conversation |
| gpt-5.4 | 9/10 | 2/2 | 2/2 | 3/3 | 1/2 | 1/1 | Open gpt-5.4 conversation |
| qwen3-coder-next | 7/10 | 2/2 | 2/2 | 2/3 | 1/2 | 0/1 | Open qwen3-coder-next conversation |
| glm-5.1 | 6/10 | 2/2 | 1/2 | 2/3 | 1/2 | 0/1 | Open glm-5.1 conversation |
How scoring works
Each run is graded on five dimensions:
- Task Completion (0-2): how fully the workflow answered the requested analysis task and completed required steps.
- Execution Correctness (0-2): whether generated code is correct, consistent, and likely runnable without major fixes.
- Output Quality (0-3): how well tables, charts, and results match the expected analytical outcomes.
- Reasoning Quality (0-2): clarity and correctness of interpretation, explanation, and conclusions.
- Reliability (0-1): robustness of the run, including low hallucination risk and stable behavior.
The final score is the sum of these dimensions, so total score = 0-10. Higher values indicate more complete, correct, and reliable notebook results for this workflow.
AI Analysis with qwen3.5:397b
This run uses AI with a step-by-step list of prompts to perform the analysis task Topic Modeling with LDA in Python. Below, you can review the full conversation generated with qwen3.5:397b. After the conversation, we show detailed score cards for this run.
Score cards for qwen3.5:397b for Topic Modeling with LDA in Python
Total score: 10/10
Task Completion: 2/2
ExcellentAll major requested steps were completed: TF-IDF vectorization, LDA with 5 topics, topic distribution plot, and sample documents with dominant topics.
Execution Correctness: 2/2
ExcellentThe code appears runnable and produced outputs for vocabulary size, topic words, plot, topic counts, and sample document topic probabilities. The only issue shown is a non-fatal FutureWarning.
Output Quality: 3/3
ExcellentOutputs match the expected outcomes semantically: vocabulary size is shown, five interpretable topic word lists are printed, a bar chart is created, and five sample documents are labeled with dominant topics.
Reasoning Quality: 2/2
ExcellentThe workflow correctly explains the steps and interprets the topics in a sensible way. It also uses the topic distribution to derive dominant topics for sample documents.
Reliability: 1/1
ExcellentThe workflow is consistent and mostly robust; the only minor concern is the use of TF-IDF with LDA, but the notebook still produces coherent results.
Try MLJAR Studio
Run the same type of AI-powered data analysis on your own datasets with conversational notebooks in MLJAR Studio.
Explore More AI Analysis Examples
Discover additional workflows across categories. Each example includes prompts, conversation outputs, and model-level scoring so you can compare approaches and results.