Skip to content
Tutorials Center
My learning

Topics

Lesson 73 of 111 ยท Beyond the Basics

Recommendation Basics

Build a recommender from scratch: user-item matrices, similarity-based collaborative filtering, matrix factorization with SVD and NMF, cold start, and honest evaluation.

"People who liked this also liked that." Recommenders choose which movies, songs, products or articles to show each person, and they are one of the most commercially important uses of machine learning. The core data is not a table of features and a label. It is a user-item matrix: rows are users, columns are items, and a cell holds a rating, a purchase or a click, with most cells empty because nobody has tried most things.

This lesson builds the two classic approaches from scratch with NumPy and scikit-learn, using a small synthetic ratings matrix so everything runs in the browser: similarity-based collaborative filtering and matrix factorization. It also covers the cold-start problem and the one evaluation rule that matters most: always test on ratings the model has not seen.

The recommender setup

Users Rows of the matrix.
Items Columns of the matrix.
Ratings Mostly missing.

The task is to fill in the blanks.

1 / 3

A synthetic ratings matrix

Real rating data has structure: people share tastes. We simulate this with two hidden ingredients: each user has a taste vector, each item has a style vector, and the rating is their match plus noise, clipped to 1 to 5. Then we hide 30% of the ratings as a test set and treat the rest as the only observed data.

Python Build the matrix and the baselines

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.

Predicting the global average rating gives an RMSE of about 1.2, and item averages are no better here (about 1.23), because with so few ratings per item the item means are noisy. Those two numbers are your baselines, as in Baselines and Dummy Models: a recommender that cannot beat "the average rating of this item" is not worth deploying.

Similarity-based collaborative filtering

The idea of user-based collaborative filtering: to predict how user u will rate item i, find users similar to u who have rated i, and average their ratings, weighting by similarity. Similarity is usually the cosine between two users' rating vectors after subtracting each user's average (some people rate everything high; centering removes that habit). Item-based filtering does the same with columns: similar items are those rated alike by the same people.

Python User-based collaborative filtering

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.

One neighbour is a noisy estimate (RMSE about 1.2, no better than the baseline); with 5 neighbours it drops to about 0.98, and more neighbours help only a little beyond that. Each prediction here loops over every cell, which is fine for a toy matrix but is exactly why real systems use precomputed neighbour lists or factorization: millions of users cannot all be compared with each other at request time.

Matrix factorization

A different view: suppose ratings are driven by a few hidden dimensions, such as "likes action" or "prefers old films", for both users and items. Then the big matrix is approximately a product of two thin matrices: a users x k matrix and a k x items matrix. Matrix factorization learns those two factors, and every missing rating is predicted by the dot product of that user's and that item's factors. This is the same compression idea as PCA and SVD, applied to a matrix with holes.

The catch is the holes. TruncatedSVD and NMF expect a complete matrix, so a quick approach fills missing cells with something neutral (here: the user's average, after centering that means 0) and factorizes the result. This trick is a quick start, but, as the numbers below show, it is clearly weaker than fitting only the observed cells, because the made-up fill values pull every prediction toward the average. Production systems instead fit the factors only to observed cells, using gradient descent or alternating least squares.

Python Factorization: SVD, NMF and a masked fit

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 mean-filled SVD reaches its best RMSE (about 1.05) at k=3, the number of hidden dimensions we simulated, and then gets worse as extra factors fit noise; NMF lands in the same range. The masked ALS fit is much better, about 0.64 at k=3, and slightly worse (about 0.70) at k=8. Fitting only the observed cells beats filling holes, and both curves bottom out near the true number of factors. The reg penalty in the ALS version plays the same role as regularization in Ridge and Lasso: without it, factors fit the few observed ratings of each user too closely.

Content-based filtering in code

Collaborative filtering uses who liked what. Content-based filtering uses what the items are. It describes each item by its features (genres, keywords, price, tags, an embedding of its text or image), builds a user profile from the items the user liked (usually the average of their feature vectors) and recommends the items most similar to that profile, using cosine similarity (vectors and similarity).

Its strengths are exactly the weaknesses of collaborative filtering: it works for a new item with no ratings (the features exist from day one), it needs no data about other users, it respects privacy and it can explain itself ("because you liked sci-fi with space travel"). Its weaknesses are a filter bubble (it recommends more of the same and cannot discover a taste the user has never shown), the work of building good features, and a new user still needs some history. Real systems blend both in hybrid recommenders.

Python A content-based movie recommender with TF-IDF and cosine similarity

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 sci-fi fan receives Space Cook, Robot Heart and Moon Colony (all have "sci-fi" among their tags), the cooking fan receives the baking competition first, and the viewer who liked the cross-over comedy receives Galaxy Run, Star Voyage and Kitchen Wars, titles from both worlds, because the profile contains words from both. The model needed no other user's ratings, and a brand-new movie could be added to the table and recommended immediately. To evaluate a content-based recommender, hide some of each user's liked items and measure how often they appear in the top n (see the section on evaluation below).

Cold start

All of the above fails for a brand-new user or item: there is nothing to compare. This is the cold-start problem. Practical fixes, roughly in order of effort:

  • Show popular or highly rated items to everyone new.
  • Ask for a few starting preferences at sign-up (pick three genres you like).
  • Use content features (genre, price, text, image embeddings) to recommend items similar to what little is known, a content-based approach that needs no other users.
  • Blend: start with popularity and content features, and move to collaborative filtering as ratings accumulate.

Evaluating a recommender

Three rules prevent the usual mistakes.

  1. Hold out ratings and predict them. Never score the model on cells it was trained on, as we did above. For real data, split by time (train on the past, test on the future) when possible, as in cross-validation for time.
  2. Use the right metric for the job. RMSE measures rating accuracy, but a shop rarely shows predicted ratings. It shows a ranked top-N list, which is judged with ranking metrics: precision at k, recall at k, or NDCG (does the item the user actually chose appear near the top?).
  3. Watch for popularity bias. A model that recommends only blockbusters scores well on average and serves nobody's niche tastes. Check coverage (how many items are ever recommended) and diversity.

Offline numbers are not the final judge. Whether recommendations raise clicks, sales or satisfaction is settled by an A/B test on real users.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In user-based collaborative filtering, how is a missing rating predicted?

    Similar users act as stand-ins. Their (centered) ratings for the target item are averaged with similarity weights.
  2. 2. What does matrix factorization assume about the ratings matrix?

    Users and items are described by a few latent dimensions; a rating is the dot product of the user's and item's factor vectors.
  3. 3. Which is a valid way to handle a brand-new user with no ratings?

    Cold start needs a fallback that does not depend on the user's own history: popularity, onboarding questions, or content-based similarity.
  4. 4. Why must you evaluate on held-out ratings?

    Training error is optimistic, especially for flexible factorizations. The hidden cells play the role of the future.

Summary

  • Recommenders work on a sparse user-item matrix; the task is to predict the empty cells or rank unseen items.
  • Collaborative filtering borrows from similar users (or items); center ratings and use cosine similarity.
  • Matrix factorization compresses the matrix into a few latent factors; fit it on observed cells and regularize it.
  • Beat the global-mean and item-mean baselines before claiming success.
  • Cold start needs popularity, onboarding or content features.
  • Evaluate on hidden ratings, prefer ranking metrics for top-N lists, and confirm with live tests.

Next, forecasting with the models you already know: Machine Learning for Time Series.