Contents/ Part XXIV · Advanced & Applied Topics/ Chapter 147

Recommendation Systems

Every feed, storefront, and streaming home page is a recommender guessing what you want next. Underneath the personalization is a strikingly simple object, a giant, mostly-empty table of who liked what, and a clever way to fill in the blanks. This chapter builds that intuition from a similarity score up to the latent-factor models that won the Netflix Prize.

⏱️ ~24 min read
🐍 Notebook included
📊 Chapter 147

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 recommendation system predicts a user's preference for items they have not seen, from a sparse user-item matrix. Content-based methods match items by their features; collaborative filtering uses the wisdom of similar users; matrix factorization learns hidden latent factors for both.
🧩
Sparsity is the whole challenge

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.

1

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.

Users × items: fill the blanks, then recommend the highest Movie AMovie BMovie CMovie DMovie E AvaBenCaraDan 5 ? 4 ? 4.6? ? 2 3 ? ? 4 ? ? 5 4 ? 1 ? 4 ? predicted → recommend! most cells are blank; a good model estimates them and ranks the best unseen items per user

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.

2

Content-Based vs Collaborative Filtering

The two classic families differ in what they lean on: the items' features or other users' behavior.

Content-based: match item features you liked a sci-fi thriller... liked: SciFi rec: more SciFi uses only YOUR history + item tags works for new items; can be too narrow Collaborative: match similar users you similar tastes rec: what THEY liked needs no item features; struggles on brand-new items
3

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.

R ≈ P × Qᵀ · a rating is a user's factors dotted with an item's R ratings (mostly blank) users P user factors × Qᵀ item factors items → r̂ᵘᵢ = pᵘ · qᵢ e.g. [0.9, −0.2, 0.4] · [0.7, 0.1, 0.5] = predicted stars

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.

4

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.

📂 Dataset · recommendation-systems--movie-ratings.xlsx

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.

5

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.

ApproachHow it worksWhere it shows up
Neighborhood methodsUser-user or item-item similarity (cosine, Pearson) over the interaction matrix“Customers who bought this also bought”
Matrix factorizationLatent factors via SVD, ALS, or SGD, with bias terms and implicit-feedback weightingThe classic personalization workhorse
Two-tower neural modelsDeep embeddings for user and item; a dot product retrieves candidates at scaleLarge-scale candidate retrieval (YouTube, ads)
Learning to rankScore and order the retrieved candidates by predicted engagementThe ranking stage of every feed
Sequential / session modelsTransformers over a user's recent actions to predict the next oneNext-video, next-song, session-based rec
Implicit feedbackLearn from clicks, views, and dwell time (not just ratings), weighting confidenceNearly all real systems, where stars are rare
🔬 Research frontier

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 Notebook (code & outputs) ▶ Open in Colab ⬇ View / Download on GitHub

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.
6

Practice Challenges

Five exercises on the movie-ratings data. Full solutions are in the companion solutions notebook.

1

Build the matrix

Pivot the long ratings table into a user-by-movie matrix and report its sparsity (fraction of cells filled).

Hint: df.pivot_table(index, columns, values).
2

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?

Hint: RMSE = sqrt(mean of squared errors).
3

Item-item similarity

Compute cosine similarity between movies and list the five most similar to a chosen film. Do their genres agree?

Hint: cosine_similarity on the item vectors.
4

Train matrix factorization

Fit a biased MF model with SGD (K = 3) and report its test RMSE. Confirm it beats both baselines.

Hint: predict = mu + b_u + b_i + p_u · q_i.
5

Recommend for a user

For one user, predict every unseen movie, drop the ones they have rated, and print the top 5 recommendations.

Hint: sort predicted ratings, exclude seen items.
📓

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.

📓 View Solutions ▶ Open in Colab ⬇ GitHub
7

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.

➡️
Up next

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.