Skip to content
Tutorials Center
My learning

Topics

Lesson 105 of 111 ยท Projects

Project: Movie Recommender

Build a movie recommender end to end with NumPy and pandas: bias baselines, genre profiles, item-item filtering, matrix factorization, ranking metrics and cold start.

"What should I watch tonight?" A movie recommender answers that question for every user, and building one is the best way to see why recommendation is different from ordinary prediction. In Recommendation Basics you met user-based collaborative filtering and matrix factorization on a small matrix. This project takes the full journey with a realistic data set: split the ratings by time, build several recommenders, evaluate them with both rating-error and ranking metrics, produce an actual list for a user, and handle a brand-new user.

The data is generated with a fixed seed so that every number matches what you see when you run it: 300 users, 90 movies, about 7,300 ratings from 1 to 5. Each movie has one or two genres, a hidden quality, and a popularity (popular movies are rated by more people). Each user has genre tastes and a personal generosity. Real data such as the MovieLens collection would need a download, and the method is exactly the same.

Four recommenders

Popularity Show what everyone watches.
Content-based Match a user's taste to genres.
Collaborative Similar movies, similar people.
Factorization Hidden taste factors.

Then combine them.

1 / 2

Step 1: Split by time and build the simple models

Split by time, not at random. A recommender's job is to predict what a user will like next, so each user's most recent 20% of ratings go to the test set, and the rest is training data. A random split would let the model learn from a user's future ratings.

Two different questions. A recommender can be judged on rating prediction (RMSE: how close is the predicted rating to the real one?) or on ranking (does the top-5 list contain movies the user liked?). They are not the same, and the example shows both. For ranking we use precision@5 (the share of the top 5 that the user liked, meaning rated at least 4) and the hit rate@5 (the share of users with at least one hit), counting only movies the user had not already rated.

The first TryIt builds four recommenders and puts them side by side:

  • Popularity: everyone gets the most-rated movies. Nothing is personal, but this baseline is much harder to beat than people expect.
  • Bias model: predicted rating = global mean + a movie bias (is it a good movie?) + a user bias (is the person generous?). Biases are shrunk toward 0 when there are few ratings, so a movie with two 5-star ratings is not crowned the best.
  • Genre profile (content-based): for each user, average the genres of the movies they rated, weighted by how much they liked each one compared with their usual level. The affinity between a user's profile and a movie's genres is added to the bias model.
  • Hybrid: the chance that a user rates a movie (popularity) times the chance that they like it if they do (the rating model).
Python Time split, baselines, genre profiles and a hybrid

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The rating-error numbers and the ranking numbers tell different stories. For rating prediction, the global mean has an RMSE of 1.152, the bias model gets 0.979 and adding the genre profile brings it to 0.766, a clear gain from personalization. But for ranking, plain popularity (precision@5 of 0.175, hit rate 0.614) beats both personalized models (about 0.09 and 0.38). Why? The bias model recommends movies with a high estimated rating, and the best of those are often niche movies, while the movies the users actually go on to rate and like are mostly popular ones. A model that is good at saying "this user would rate the movie 4.2 if they watched it" is not automatically good at saying "this user will watch it next". The hybrid, popularity times the probability of liking, wins at ranking (precision@5 0.204, hit rate 0.697): it recommends popular movies, but picks the ones that match the user's taste. That is why production systems have separate stages for candidate generation (what will they probably watch) and ranking (which of those will they like).

Step 2: Item-item filtering, matrix factorization and recommendations

Two stronger collaborative models come next.

Item-item collaborative filtering measures how similar two movies are by comparing the columns of the rating matrix (after removing each user's average), using cosine similarity. To predict a rating, it takes the user's ratings of the 20 most similar movies, weighted by similarity. Item-item methods are popular because movie similarities are more stable than user similarities.

Matrix factorization learns a short vector (here 8 numbers) for each user and each movie, plus biases, so that the dot product of the user vector and the movie vector, added to the biases, reproduces the known ratings. Training uses stochastic gradient descent with L2 regularization. This is the idea behind the SVD models that won the Netflix Prize; see Other Dimensionality Reduction for the linear algebra it relies on.

The example evaluates both, turns the factorization into a hybrid ranking, prints a recommendation list for one user (with the movie's predicted rating and how many people rated it), and ends with the cold-start case.

Python Item-item kNN, matrix factorization, recommendations and cold start

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Matrix factorization is the best rating predictor with an RMSE of 0.727, against 0.881 for item-item (and 0.766 for the genre profile). Item-item filtering is weaker here because each movie is rated by a modest number of users, so its similarities are noisy. Ranking follows the pattern from step 1: the raw rating predictors rank poorly (precision@5 of 0.077 and 0.062), and the hybrid built on the factorization ratings gets the best score, with precision@5 of 0.213 and a hit rate of 0.709. The list for user 7 shows the idea in practice: five movies that many people watched, of genres the user's own top-rated movies share (horror, scifi, comedy), with predicted ratings of 3.5 to 4.1.

Cold start: users and movies with no history

A new user has no ratings, so neither collaborative method can say anything. Practical fixes, from simplest to most elaborate:

  • Ask at sign-up. The last block of the example does what many services do: the user ticks favorite genres and the recommender ranks movies by genre match, shifted toward movies with a high average rating and some popularity. The top five are mostly scifi or horror movies (plus one very well-rated drama), with average ratings of 3.9 to 4.4. That is a reasonable first screen, and every rating the user gives afterwards makes the personalized models take over.
  • Show popular, well-rated items and learn from what the user clicks.
  • Use content for new movies. A new movie has no ratings, but it has genres, a cast and a description, so a content-based model can recommend it until it collects ratings.

What a production system adds

  • Implicit feedback. Most real data is clicks, plays and purchases, not stars. A missing interaction does not mean dislike.
  • Larger candidate sets and approximate search. With millions of items you cannot score everything; vectors from a factorization are searched with fast nearest-neighbor indexes.
  • Diversity and fairness. A list of five near-identical movies is a bad list, and a system that only recommends the popular items starves the rest of the catalog (Fairness, Bias and Responsible ML).
  • Online evaluation. Offline metrics are a filter. The final judge is an A/B test on real users, with metrics such as watch time and return visits.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the project split each user's ratings by time instead of randomly?

    At prediction time you know a user's past and want to guess what comes next. Holding out each user's latest ratings mimics that, and avoids leaking future information into training.
  2. 2. A model has the lowest RMSE but a poor precision@5 compared with plain popularity. What does that mean?

    RMSE measures rating accuracy over rated movies. Ranking also depends on which movies the user will actually watch, and that follows popularity. A hybrid of the two can beat both.
  3. 3. Why are movie and user biases shrunk toward zero when there are few ratings?

    Averages over few ratings are noisy. Damping blends the estimate with zero in proportion to how little evidence there is.
  4. 4. A brand-new user has no ratings. Which approach works immediately?

    Collaborative methods need ratings. Content signals (genres picked at sign-up) and popularity work from the first minute, and the personalized models take over as ratings arrive.

Summary

  • Split ratings by time per user, and evaluate both rating error (RMSE) and ranking (precision@k, hit rate@k) on movies the user had not rated.
  • Popularity is a strong ranking baseline; bias models, genre profiles, item-item filtering and matrix factorization improve rating prediction.
  • Matrix factorization with biases learns hidden taste factors with SGD and was the best rating predictor here.
  • A hybrid of popularity and the probability of liking beats either part alone at ranking.
  • Cold start needs content, onboarding questions and popularity until ratings arrive; production systems add implicit feedback, candidate retrieval, diversity and A/B tests.

Next, the classic starter project: Project: Titanic Survival.