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 gpt-5.4
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 gpt-5.4. After the conversation, we show detailed score cards for this run.
Score cards for gpt-5.4 for Topic Modeling with LDA in Python
Total score: 9/10
Task Completion: 2/2
ExcellentAll major requested steps were completed: data loading, TF-IDF vocabulary size, 5-topic LDA with top words, topic distribution plot, and a 5-document dominant-topic table.
Execution Correctness: 2/2
ExcellentThe provided code is coherent and likely runnable as shown. It uses valid sklearn, pandas, and plotting calls without obvious syntax or logic errors.
Output Quality: 3/3
ExcellentOutputs match the expected outcomes semantically: vocabulary size is shown, five topic word lists are printed, a bar chart is produced, and five sample documents are labeled with dominant topics.
Reasoning Quality: 1/2
Needs workThe workflow includes some correct reasoning, such as noting that LDA works better on counts than TF-IDF, but it also mixes in unnecessary commentary and does not fully align with the prompt's TF-IDF instruction.
Reliability: 1/1
ExcellentThe workflow is reasonably consistent and produces interpretable results, though it deviates from the prompt by fitting LDA on CountVectorizer rather than the TF-IDF representation.
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.