Build a Content-Based Movie Recommender from Scratch
Machine LearningModel answerLast updated
Design and evaluate a simple content-based filtering system that recommends movies to a new user who has rated only a handful of films.
Scenario
You are a junior ML engineer at a streaming platform. The product team wants a recommendation module for brand-new users who have rated fewer than five movies. Collaborative filtering is not viable yet because there is not enough user-interaction data (the classic cold-start problem).
Your manager asks you to prototype a content-based filtering approach using only movie metadata that the platform already has:
- Genre tags (e.g., Action, Drama, Sci-Fi) — multi-label
- Average critic score (0–100)
- Release decade
- Runtime in minutes
- Director name
What you are expected to cover
- Problem framing — what exactly are you predicting or ranking?
- Feature engineering — how do you turn the metadata above into a usable item representation?
- User profile construction — given a few rated movies, how do you represent the user?
- Scoring and ranking — how do you produce a ranked list of recommendations?
- Evaluation — how do you measure whether your system is any good, offline and online?
- Limitations and next steps — what does this approach get wrong, and what would you do with more data?
You do not need to write production code, but pseudo-code or concrete math notation is encouraged where it clarifies your design.
Hints
Hint 1
Think about how to represent a movie as a single numeric vector — which fields need encoding and which can be used directly?
Hint 2
The user profile can be as simple as an average of the item vectors for movies the user rated positively — what weighting scheme could make this smarter?
Hint 3
Offline evaluation is tricky when you only have a handful of ratings per user — consider what metrics still make sense at very small k values.
Model answer
Easy Try it yourself first — then compare your approach, complexity and edge cases.
1. Problem Framing
The task is item ranking: given a target user and a catalog of N movies, produce an ordered list of the top-k movies the user is most likely to enjoy. We are not predicting an explicit star rating; we are scoring relevance so we can rank.
Success metric at the product level: click-through rate and watch-through rate on the recommended row.
2. Feature Engineering — Item Representation
Each movie becomes a fixed-length vector by concatenating:
| Field | Encoding | Notes |
|---|---|---|
| Genre tags | Multi-hot vector (e.g., 20 genres) | A movie can belong to several genres |
| Critic score | Single float, scaled to [0, 1] | Divide by 100 |
| Release decade | One-hot (e.g., 1950s–2020s → 8 bins) | Avoids treating decade as a continuous ordinal |
| Runtime | Single float, standardized (subtract mean, divide by std) | Prevents large values dominating |
| Director | Hashed or label-encoded integer, then optionally embedded | For a simple prototype, skip or use a binary "same director" feature at scoring time |
Resulting item vector v_i has ~30 dimensions. No neural network needed at this stage.
Normalization matters: before computing similarity, L2-normalize each v_i so cosine similarity reduces to a dot product and no single feature dominates by scale.
3. User Profile Construction
Given movies the user has rated, build a weighted average of their item vectors:
u = sum(w_j * v_j for j in rated_movies) / sum(w_j)
Where w_j is the rating (e.g., 1–5 stars). This makes movies the user loved contribute more than movies they disliked.
A refinement: subtract the global mean rating from w_j first (use w_j = r_j - r_mean). Negative weights then down-weight genres the user dislikes — a meaningful signal even with only three or four rated movies.
4. Scoring and Ranking
For each unrated movie i in the catalog, compute:
score(i) = cosine_similarity(u, v_i)
= dot(u, v_i) # because both are L2-normalized
Return the top-k movies by descending score, excluding already-rated movies.
Practical filter: also exclude movies with a critic score below, say, 40 — a cheap quality floor that improves perceived recommendation quality with zero ML cost.
5. Evaluation
Offline
Because each new user has only a few ratings, standard metrics at large k are noisy. Use:
- Precision@k (k = 3 or 5): of the top-k recommendations, how many did the held-out user actually rate positively? Hold out one of the user's rated movies, recommend, check if it appears in top-k.
- Mean Reciprocal Rank (MRR): where in the ranked list does the held-out positive item appear? Useful when there is only one ground-truth positive.
- Leave-one-out cross-validation over all users with ≥ 3 ratings.
Avoid Recall@k here — with only 4 or 5 known positives in total, recall is uninformative.
Online (A/B test)
Split new users randomly:
- Control: a popular-items baseline (top-rated movies globally).
- Treatment: content-based recommendations.
Primary metric: 7-day watch-through rate on the recommended row. Guard-rail metric: session length and next-day retention.
Run for at least two weeks to capture weekend vs. weekday variation.
6. Limitations and Next Steps
| Limitation | Impact | Next step |
|---|---|---|
| Over-specialization | Recommends movies identical to what the user already watched; no serendipity | Add a diversity re-ranking step (MMR — Maximal Marginal Relevance) |
| Ignores social signal | A critically-panned cult classic with huge audience love is under-ranked | Blend in average user rating alongside critic score |
| Cold-start on directors | New director → unknown feature | Fall back to genre-only vector for unknown directors |
| Static profile | User profile does not update within a session | Recompute u after each rating event |
| Scalability | Scoring all N movies linearly is fine at 10K items but slow at 1M | Move to approximate nearest neighbor (e.g., FAISS) once catalog grows |
With more data: once a user accumulates 20+ interactions, blend content-based scores with collaborative filtering scores (a hybrid model). Matrix factorization or a two-tower neural model can then capture latent tastes that metadata alone cannot express.
Common Mistakes to Avoid
- Not normalizing features: raw runtime (90–180 minutes) will swamp a binary genre flag (0 or 1) in cosine similarity calculations.
- Using only positive ratings to build the profile: ignoring low ratings throws away a strong negative signal.
- Evaluating with Precision@20 on users with 3 ratings: this makes every system look bad and is not actionable.
- Recommending already-watched movies: always filter the catalog before scoring.