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-oss:120b
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-oss:120b. After the conversation, we show detailed score cards for this run.
Score cards for gpt-oss:120b for Topic Modeling with LDA in Python
Total score: 10/10
Task Completion: 2/2
ExcellentAll four requested steps were completed: vocabulary size was shown, LDA with 5 topics was fit and top words printed, topic distribution was plotted, and a 5-document dominant-topic table was produced.
Execution Correctness: 2/2
ExcellentThe code is coherent and likely runnable: it uses CountVectorizer for LDA, computes document-topic distributions, and builds the requested outputs without obvious syntax errors.
Output Quality: 3/3
ExcellentThe outputs match the expected outcomes semantically, including the vocabulary size, five topic word lists, a topic prevalence bar chart, and a document excerpt/topic table.
Reasoning Quality: 2/2
ExcellentThe workflow correctly notes that LDA should use count data rather than TF-IDF and follows that reasoning consistently through the analysis.
Reliability: 1/1
ExcellentThe workflow is reasonably robust and reproducible, with a fixed random seed for sampling and standard sklearn methods.
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.