Recommendation Systems
Build recommendation engines - collaborative filtering, matrix factorization with implicit, content-based filtering, hybrid systems, and offline evaluation.
skill: Recommendation Systems category: AI & ML tags: [Recommendation Systems, Collaborative Filtering, Matrix Factorization, Content-Based, RecSys] readingTime: 5
The best recommendation system is the simplest one that meets your business goals. Popularity-based ("trending items") beats collaborative filtering for new users; collaborative filtering beats content-based for discovery. Production systems are almost always hybrid. Start with baselines; add complexity only when you can measure the improvement.
Workflow
- Build a collaborative filtering baselineCollaborative filtering recommends items that similar users liked. It requires no item content metadata - just user-item interactions.
- Apply matrix factorization for implicit feedbackExplicit ratings (1-5 stars) are rare; implicit feedback (clicks, views, purchases) is abundant. Matrix factorization learns user and item latent factors from implicit signals.
- Build content-based filteringContent-based filtering recommends items similar to what the user liked, based on item features. It handles new items well (no cold-start) but doesn't enable discovery across content types.
- Build a hybrid recommendation systemHybrid systems combine collaborative and content-based to get the benefits of both - discovery from collaborative + cold-start handling from content-based.
- Evaluate recommendation qualityOnline metrics (CTR, revenue per impression) are the ground truth, but offline evaluation guides model development. Using the right offline metric avoids optimizing for the wrong thing.
Output Format
RecSys: user-user cosine similarity baseline, implicit ALS for implicit feedback, TF-IDF content similarity, hybrid CF+CB weighting, and NDCG@K + MRR offline evaluation with temporal split.
user_item_sparse = csr_matrix((counts, (user_ids, item_ids)))
ALS: implicit.als.AlternatingLeastSquares(factors=64, iterations=20)
TF-IDF: TfidfVectorizer(ngram_range=(1,2)) + linear_kernel(matrix, matrix)
Hybrid: cf_weight * cf_scores + cb_weight * cb_scores
NDCG@K: 1/log2(rank+2) for hits in top-K, normalized by ideal DCGExamples
Edge Cases & Gotchas
- Cold-start problem - New users have no interaction history → no CF recommendations. Solutions: (1) Ask for explicit preferences during onboarding, (2) Use popularity-based recommendations, (3) Content-based on stated interests.
- Popularity bias - CF naturally over-recommends popular items. Most users never interact with long-tail items, so CF can't discover them. Add diversity: re-rank to include at least 20% long-tail items.
- Feedback loops - Recommended items get more views → more interactions → recommended more. This feedback loop narrows recommendations over time. Monitor item coverage and diversity metrics; introduce random exploration.
- Imbalanced implicit signals - Views are noisy (low intent); purchases are sparse (high intent). Weight signals: purchase=5, add_to_cart=3, view_30s=1, view_2s=0.1. Use domain knowledge to set weights before training.
When to Break These Rules
- Use two-tower neural network embedding models (Google's DSSM, YouTube DNN) instead of ALS for very large catalogs (>10M items) where ALS becomes slow and embeddings can be served via approximate nearest neighbor.
- Use LLM-based recommendations (embedding items with a language model, retrieving via semantic similarity) for text-heavy content (articles, documentation, products with detailed descriptions) where semantic similarity drives relevance.
Reference Files
https://github.com/benfred/implicithttps://arxiv.org/abs/1606.07792