Discovers the main themes in the text descriptions of popular (voted) Kaggle datasets. Two unsupervised topic models are applied and compared:
- LDA (Latent Dirichlet Allocation): probabilistic model built with Gensim and explored interactively with pyLDAvis
- NMF (Non-negative Matrix Factorization): matrix factorization on TF-IDF features, visualized with word clouds
After running the script, the results are saved in the repo:
| Output | Description |
|---|---|
outputs/topics_summary.md |
Top words for every LDA and NMF topic, plus the LDA coherence score |
outputs/lda_visualization.html |
Interactive intertopic distance map (live demo) |
images/ |
One word cloud per NMF topic, plus an overview image |
data/voted-kaggle-dataset.csv is the Upvoted Kaggle Datasets collection by canggih: 2,150 Kaggle datasets with at least two votes, collected on 26 February 2018. Each row has fields such as title, subtitle, owner, votes, tags, license, views, downloads and Description. The Description column is the text used for modeling.
Source: Upvoted Kaggle Datasets (listing on Baselight). Please check the original Kaggle page for the dataset's license before reusing the data.
- Data loading: read the CSV with pandas.
- Cleaning: drop missing, empty and whitespace-only descriptions.
- Preprocessing
- Lowercase and tokenize with Gensim
simple_preprocess(accents removed, tokens under 2 characters dropped) - Remove stop words (scikit-learn's English list plus custom words such as using, use, used, also, new, like)
- Drop documents that become empty
- Lowercase and tokenize with Gensim
- LDA
- Build a Gensim
Dictionaryand filter extremes (words in fewer than 2 documents or more than 90% of documents) - Train
LdaModelwith 5 topics (10 passes, 100 iterations,random_state=42) - Compute the c_v coherence score
- Visualize with pyLDAvis (metric MDS) and export to HTML
- Build a Gensim
- NMF
- Build a TF-IDF matrix (max 5,000 features)
- Factorize with
NMF(8 topics, 400 iterations,random_state=42) - Extract the top 10 words per topic and draw a word cloud of the top 20
The topic counts differ (LDA 5, NMF 8) and can be changed with command-line options. For a fair comparison, run both with the same number.
Requirements: Python 3.11 or newer. requirements.txt pins the exact library versions the project was run with, so other versions are not guaranteed to work.
git clone https://github.com/AshfanBelim/Topic-Modeling.git
cd Topic-Modeling
python -m venv .venv
.venv\Scripts\activate # Windows
source .venv/bin/activate # macOS / Linux
pip install -r requirements.txt
python topic_modeling.pypython topic_modeling.py --lda-topics 6 --nmf-topics 6
python topic_modeling.py --data path/to/file.csv --text-column Description| Option | Default | Description |
|---|---|---|
--data |
data/voted-kaggle-dataset.csv |
Path to the CSV file |
--text-column |
Description |
Column containing the text |
--lda-topics |
5 |
Number of LDA topics |
--nmf-topics |
8 |
Number of NMF topics |
--top-words |
10 |
Words listed per topic |
--cloud-words |
20 |
Words drawn in each word cloud |
.
├── topic_modeling.py # Full pipeline (LDA + NMF)
├── requirements.txt
├── data/
│ └── voted-kaggle-dataset.csv
├── outputs/ # LDA visualization and topic summary
├── images/ # NMF word clouds
├── LICENSE
└── README.md
Python, pandas, Gensim, scikit-learn, pyLDAvis, matplotlib, WordCloud
- Choose the number of topics using coherence scores
- Add lemmatization and bigrams
- Label topics manually and compare LDA vs NMF quantitatively
Ashfan Belim, GitHub
MIT, see LICENSE.
