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 glm-5.1
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 glm-5.1. After the conversation, we show detailed score cards for this run.
Score cards for glm-5.1 for Topic Modeling with LDA in Python
Total score: 6/10
Task Completion: 2/2
ExcellentAll major requested steps were attempted: TF-IDF vectorization, 5-topic LDA, topic distribution plot, and sample document topic dominance. The notebook also shows vocabulary size and topic-word lists.
Execution Correctness: 1/2
Needs workThe code appears runnable and produced outputs, but it uses LDA directly on TF-IDF, which is methodologically weak and led to degenerate topic behavior. The sample document table is also incomplete in the provided output.
Output Quality: 2/3
GoodThe expected outputs are mostly present: vocabulary size, five topic word lists, a topic distribution plot, and a sample of five documents with dominant topic labels. However, the topic quality is poor and the final sample table is only partially shown.
Reasoning Quality: 1/2
Needs workThe assistant correctly noted that LDA is better suited to count data and that the topics were noisy, but the workflow still proceeded with TF-IDF without addressing the issue. The reasoning is mostly descriptive rather than corrective.
Reliability: 0/1
Needs workThe workflow is fragile because it applies LDA to TF-IDF and produces highly degenerate topics dominated by stopwords and rare tokens. This reduces confidence in the robustness of the analysis.
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.