Simple Python implementation of collaborative topic modeling?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Collaborative Topic Modeling (CTM) combines collaborative filtering (user-item interactions) with topic modeling (text content analysis) to make recommendations. It uses Latent Dirichlet Allocation (LDA) to extract topics from item descriptions, then integrates those topics with user preference vectors through matrix factorization. This hybrid approach handles the cold-start problem better than pure collaborative filtering.
How CTM Works
CTM has two components that work together:
- Topic Model (LDA): Extracts topic distributions from item content (articles, product descriptions, papers)
- Collaborative Filter (Matrix Factorization): Learns user preference vectors that align with topic distributions
The key insight: instead of learning arbitrary latent factors for items (as in standard matrix factorization), CTM constrains item factors to be close to their LDA topic distributions.
Step 1: Prepare the Data
Step 2: Extract Topics with LDA
Step 3: Collaborative Topic Model
Step 4: Train and Predict
Step 5: Evaluate
CTM vs Standard Matrix Factorization
| Feature | Standard MF | CTM |
| Cold start | Cannot recommend new items | Uses LDA topics for new items |
| Content awareness | No | Yes — uses item text |
| Interpretability | Latent factors are opaque | Factors correspond to topics |
| Data needed | Only user-item matrix | User-item matrix + item text |
Common Pitfalls
- Lambda balance:
lambda_vcontrols how strongly item factors are pulled toward LDA topics. Too high and the model ignores user preferences. Too low and it becomes standard matrix factorization. Tune with cross-validation. - LDA quality: CTM is only as good as its topic model. If LDA produces poor topics (too few or too many), the recommendations suffer. Tune
n_topicsusing coherence scores. - Sparse interactions: With very few ratings per user, the user factors are poorly estimated. Increase
lambda_uregularization for sparse data. - Scaling: The alternating least squares update loop is O(n_users * n_items * n_topics). For large datasets, use stochastic gradient descent or batch updates instead of full ALS.
- Missing vs zero: In the interaction matrix, 0 should mean "not observed," not "user dislikes item." Only train on observed entries (where
mask = R > 0).
Summary
- CTM combines LDA topic modeling with matrix factorization for content-aware recommendations
- Item latent factors are regularized toward their LDA topic distributions
- This handles the cold-start problem — new items with text but no ratings get topic-based factors
- Use alternating least squares to update user and item factors iteratively
- Tune
lambda_vto balance between content-based and collaborative signals
Related reading
- Simple way to visualize a TensorFlow graph in Jupyter?
- Simplest feature selection algorithm
- simultaneously update theta0 and theta1 to calculate gradient descent in python
- Singleton/Synchronization in Clustered environment
- Simple way to find if two different lists contain exactly the same elements?
- Simple way to measure cell execution time in ipython notebook
- Sklearn Categorical Imputer?
- Sklearn Chi2 For Feature Selection
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.