A recommender is a machine for answering one question at massive scale: given everything this person has done, what will they want next? The raw material is an interaction matrix, users down the side, items across the top, a rating or a click in a cell. The catch is that the matrix is almost entirely blank: any one user has touched a tiny fraction of the catalog. Every recommendation method is, at heart, a way to predict the missing cells and surface the highest ones.
A catalog has millions of items; a user rates a handful. The interaction matrix is often over 99 percent empty, so you cannot just average what is there. The art is borrowing strength: filling a blank cell from similar users, similar items, or a compact set of learned taste dimensions that explain the ratings you can see.
The User-Item Matrix
Lay every user's ratings in a grid. A filled cell is a known preference; a blank is what you want to predict. Fill the blanks well and recommending is easy: for each user, sort their predicted cells and show the top few.
Because most of the grid is empty, you evaluate a recommender by hiding some known ratings, predicting them, and measuring the error (RMSE for star ratings) or how many of a user's true favorites land in the top-N list (precision and recall at K). The rest of the chapter is three increasingly powerful ways to fill those blanks.
Content-Based vs Collaborative Filtering
The two classic families differ in what they lean on: the items' features or other users' behavior.
- ●Content-based: describe each item by features (genre, tags, text) and recommend items similar to what a user already liked. It needs no other users, and it handles brand-new items, but it can trap a user in a narrow bubble of the familiar.
- ●Collaborative filtering: ignore item features and lean on the crowd, “users who liked what you liked also liked X.” It is computed from similarity (often cosine) between rows (user-based) or columns (item-based) of the matrix. Powerful and feature-free, but it stumbles on the cold-start problem: a brand-new user or item has no history to match.
Matrix Factorization: Learning Latent Taste
The idea that won the Netflix Prize is deceptively simple. Approximate the giant, sparse rating matrix
R as the product of two skinny matrices: one row of latent factors per user, one per
item. Each factor is an unnamed taste dimension, learned from the data, and a predicted rating is just the
dot product of a user's factors with an item's.
You never label the factors, the model discovers them, but they often line up with things like “action versus drama” or “mainstream versus niche.” The factors are learned by gradient descent, minimizing the squared error on the observed ratings with a little regularization, plus per-user and per-item bias terms to soak up easy raters and popular items. This is the same latent-variable idea from the Structural Equation Modeling & Mixed Models chapter and the cousin of dimensionality reduction: compress thousands of items into a handful of meaningful dimensions.
Real-World Example: Movie Ratings
The companion notebook builds a recommender from the ground up, comparing three ways to fill the blanks on a real ratings table, with numpy and scikit-learn.
4,383 ratings from 300 users across 60
movies, in the standard long format (user_id, movie_id, rating 1 to
5), plus a Movies sheet with each film's genre. The user-item matrix is only about 24 percent
filled, sparse by design.
- ●Global-mean baseline: predicting everyone's average scores RMSE 0.95, the bar to beat.
- ●User + item bias: adding “this user rates high / this movie is popular” drops it to 0.85.
- ●Matrix factorization: learning just 3 latent factors per user and movie reaches RMSE 0.74, a clear win, and yields ranked top-N recommendations per user.
The progression is the lesson: a plain average is blind to who is rating and what they are rating; adding biases helps; but only the latent factors capture that a given user loves a given kind of movie. Sort each user's predicted ratings, drop the ones they have already seen, and the top of that list is the recommendation. The same machinery, scaled to millions of users and items and fed clicks instead of stars, is what drives the feeds you scroll every day.
Recommenders in Machine Learning & AI
Production recommenders have grown far past a single rating prediction, but every layer traces back to the matrix and the latent factor. The modern stack is usually retrieve then rank: cheaply pull a few hundred candidates, then score them precisely.
| Approach | How it works | Where it shows up |
|---|---|---|
| Neighborhood methods | User-user or item-item similarity (cosine, Pearson) over the interaction matrix | “Customers who bought this also bought” |
| Matrix factorization | Latent factors via SVD, ALS, or SGD, with bias terms and implicit-feedback weighting | The classic personalization workhorse |
| Two-tower neural models | Deep embeddings for user and item; a dot product retrieves candidates at scale | Large-scale candidate retrieval (YouTube, ads) |
| Learning to rank | Score and order the retrieved candidates by predicted engagement | The ranking stage of every feed |
| Sequential / session models | Transformers over a user's recent actions to predict the next one | Next-video, next-song, session-based rec |
| Implicit feedback | Learn from clicks, views, and dwell time (not just ratings), weighting confidence | Nearly all real systems, where stars are rare |
Recommendation is where much of the money and much of the scale in applied ML live, so the research is intense. Active fronts include sequential and transformer-based recommenders that model a session as a sequence, LLM-powered recommendation that reasons over item text and user intent, and hard perennial problems: the cold-start of new users and items, the feedback loops and filter bubbles that recommenders create, and how to optimize for long-term satisfaction rather than the next click. The math still rests on predicting a sparse matrix, now with far richer signals.
Build a recommender in Python
The companion notebook loads the ratings, builds the sparse user-item matrix, computes item-item cosine similarity for collaborative filtering, scores a global-mean and a bias baseline, then trains a matrix-factorization model by hand with gradient descent, compares all three by RMSE, and generates a top-N recommendation list for a sample user, every step with a plot.
View opens the rendered notebook instantly.
Open in Colab runs it live. To run locally, install numpy, pandas,
matplotlib, scikit-learn, and openpyxl (the surprise and
implicit libraries add production-grade recommenders).
🎓 Key Takeaways
- ✓A recommender predicts a sparse user-item matrix: fill the blanks, then rank the highest unseen items per user.
- ✓Content-based matches item features to your history; collaborative filtering leans on similar users, needing no features.
- ✓Matrix factorization approximates R as user-factors times item-factors; a rating is a dot product of learned latent tastes.
- ✓Bias terms matter: model “this user rates high” and “this item is popular” before the factors.
- ✓Cold start is the recurring headache: brand-new users and items have no history to match.
- ✓Modern systems retrieve then rank with two-tower and sequential neural models, usually on implicit feedback, not stars.
Practice Challenges
Five exercises on the movie-ratings data. Full solutions are in the companion solutions notebook.
Build the matrix
Pivot the long ratings table into a user-by-movie matrix and report its sparsity (fraction of cells filled).
df.pivot_table(index, columns, values).Two baselines
On a held-out split, compute RMSE for the global mean and for a user+item bias predictor. How much does bias help?
Item-item similarity
Compute cosine similarity between movies and list the five most similar to a chosen film. Do their genres agree?
cosine_similarity on the item vectors.Train matrix factorization
Fit a biased MF model with SGD (K = 3) and report its test RMSE. Confirm it beats both baselines.
Recommend for a user
For one user, predict every unseen movie, drop the ones they have rated, and print the top 5 recommendations.
Solutions notebook
All five challenges worked in code, the sparse matrix, the two baselines, item-item similarity, the trained matrix-factorization model, and a top-N recommendation list, each with a short explanation.
Quiz: Test Yourself
Eight questions on the user-item matrix, filtering strategies, matrix factorization, and cold start. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved.
Recommenders learn from what users do. Next, Chapter 148 · NLP & Large Language Models turns to what they say, how machines represent, understand, and generate human language, from word vectors to the transformer models reshaping the field.