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-coder-next
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-coder-next. After the conversation, we show detailed score cards for this run.
Score cards for qwen3-coder-next for Topic Modeling with LDA in Python
Total score: 7/10
Task Completion: 2/2
ExcellentAll four requested steps were attempted: TF-IDF vectorization, 5-topic LDA fitting with top words, topic prevalence plotting, and dominant-topic display for 5 documents.
Execution Correctness: 2/2
ExcellentThe provided code is syntactically coherent and the outputs indicate it ran successfully, producing the expected variables, topic lists, plot, and sample-document results.
Output Quality: 2/3
GoodOutputs match the requested outcomes semantically, including vocabulary size, five topic word lists, a topic distribution bar chart, and a 5-document dominant-topic table. However, the vocabulary size is 10000 rather than the expected ~50k after filtering.
Reasoning Quality: 1/2
Needs workThe workflow explanations are mostly correct and helpful, but it overstates completion in places and includes speculative interpretation of topics without strong support from preprocessing choices.
Reliability: 0/1
Needs workThe workflow is somewhat fragile: it uses TF-IDF with LDA, which is not the standard pairing, and it limits vocabulary to 10000 instead of the expected ~50k, reducing robustness to the stated task.
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.