Recommender Systems, Part 1: From Collaborative Filtering to Matrix Factorization
Open Netflix and stare at the homepage. Open Spotify and look at Discover Weekly. Open YouTube and watch "Up next" auto-queue. Open Amazon and read "Customers who bought this also bought...". Four different products, four different teams, one identical machine under the hood — a recommender that says "you, specifically, will probably want this". By the end of this part you'll know how to build one. This is Part 1 of 2: it builds the models. Part 2 puts them in front of real users, with cold start, honest evaluation and the production pipeline.
After this lesson, you will be able to:
Distinguish content-based filtering from collaborative filtering and know exactly which one you'd reach for in each business situation
Compute user-user and item-item similarities with cosine, Pearson, and adjusted-cosine, and know why item-item dominates production at scale
Understand matrix factorization as low-rank approximation of a sparse user-item matrix — SVD and Funk-SVD
Explain why Funk-SVD beat mean-fill SVD on sparse ratings, and what the SGD and ALS update rules do
In plain words: a recommender looks at what you did before and guesses what you will want next. There are only three ways to make that guess. Ask what the item is like, ask who else behaved like you, or squeeze the whole table of past behaviour into a short list of hidden tastes. This part walks through all three on tiny tables you can check by hand.
Don't worry if "matrix factorization" sounds intimidating — by the end of this part you'll see it's just "split one big sparse table into two smaller dense ones, then multiply them back". That's it. That's the whole idea that won the Netflix Prize.
Words you need10 terms
A program that picks the few items to show one person out of a catalog far too big to browse.
A table with one row per person and one column per item. A filled cell is a rating or a click. Most cells are empty, because nobody has tried most things.
Mostly empty. A matrix that is 99.99 percent sparse has a value in about one cell out of ten thousand.
A score for how closely two lists of ratings point the same way. A score of 1 means the same taste and 0 means nothing in common.
One of the K most similar users, or items. Their ratings are borrowed to make the guess.
Subtracting each person's own average rating first, so a generous rater and a harsh rater can be compared fairly.
A hidden taste axis that the model finds by itself, such as kids film against adult drama. Nobody labels it in advance.
The short list of numbers that stands for one user or one item. A prediction is the dot product of two such lists.
Splitting the big, mostly empty table into two small full tables, one for users and one for items, whose product rebuilds the ratings.
Root mean squared error. A single number for how far the predicted ratings sit from the real ones, on average.
Every consumer product with a catalog larger than 30 items needs a recommender. The math is brutal: streaming catalogs hold thousands of titles, music catalogs tens of millions of tracks, and video sites receive hours of new video every minute (exact figures change, so check current sources). No human can browse those catalogs. The recommender is the interface — it decides what you see, in what order, on every screen.
There are three broad families of recommenders, and almost every production system blends all three:
Content-based filtering. Recommend items similar to ones the user already liked, where "similar" is defined by item features (genre, tempo, color, author, …).
Collaborative filtering — recommend items that similar users liked, ignoring the items' content entirely. "People who watched X also watched Y."
Matrix factorization & embeddings — model both users and items as vectors in the same latent space; recommend by dot product. The dominant modern approach.
We'll build each one up from scratch, then look at how Netflix and Spotify glue them together.
Content-based filtering treats each item as a feature vector and each user as a weighted blend of feature vectors over items they've liked. A new item is recommended if it's geometrically close to the user's profile.
A toy example. A movie has features (is_scifi, is_romance, is_animated, decade=2010s, runtime=120). You rated Inception 5, La La Land 4, and Toy Story 5. Your profile is a weighted average of those feature vectors (heavy on is_scifi and is_animated, moderate on is_romance). A new candidate, Interstellar (heavy on is_scifi, light on everything else), scores high on cosine similarity with your profile. We recommend it.
The good
No cold start for items. A brand-new movie uploaded yesterday has features; we can score it immediately even before a single person rates it. This is huge for news, podcasts, and any catalog that grows hourly.
Interpretable. "We recommended this because you like sci-fi and 2010s movies" is an explanation we can actually show the user.
Per-user models. Each user's profile is independent — no shared training, easy to update online.
The bad
Filter bubble. The model can only recommend things that look like what you've already liked. If your profile screams sci-fi, you will never see the brilliant indie romcom that would have surprised you.
Garbage features = garbage recs. The model is only as good as the item features you engineer. For a song, "danceability" and "valence" are useful; for a t-shirt, defining the feature vector is harder.
New-user cold start. A user with zero ratings has an undefined profile vector.
What Do You Think?
A music streaming startup just launched. They have 100 million tracks (all with rich audio features) but only 1,000 users so far, and most users have liked fewer than 5 songs. Which approach will work better on day 1: content-based filtering or collaborative filtering?
#Collaborative Filtering: "People Like You Liked This"
Collaborative filtering (CF) flips the problem: instead of comparing items by their features, it compares users by their behavior, then borrows the recommendations of behavioral twins. The killer property: CF makes no assumption about what the items are. It works equally well for movies, songs, shoes, podcasts, or news articles — as long as you have a matrix of (user, item, rating) tuples.
Build the user-item matrix R: rows are users, columns are items, entries are ratings (or clicks, or watch time, or any signal). Almost all the entries are missing — that's the whole point. You have 200M users and 17,000 movies; each user has rated maybe 50 movies. R is 99.99% sparse.
CF comes in two flavors depending on which axis you compare along.
For a target user u, find the K most similar users (their "neighbors"). Predict u's rating for item i as a weighted average of what those neighbors rated i.
The classical similarity is Pearson correlation over commonly-rated items:
Treat a user's row in R as a vector in item-space. Cosine similarity between two such vectors is the dot product (after normalization). When we say "users u and v are similar", we mean their rating vectors point in roughly the same direction. The visualization above shows exactly that geometric picture — dot product is compatibility.
Where cosine similarity itself comes from (the dot product equals the product of the lengths times the cosine of the angle) is derived in the Vectors & Spaces lesson, so it is not repeated here. What is new in this lesson is that the Pearson formula above is that same cosine, applied after a subtraction.
Amazon's 2003 paper "Item-to-item collaborative filtering" flipped the axis. Instead of finding similar users (an expensive search across hundreds of millions of users), precompute the item-item similarity matrix: how often do items i and j co-occur in users' rated sets, and with what agreement?
To predict u's rating on item i: look at items u has already rated, weight their ratings by how similar each of those items is to i, and take the weighted average.
r^ui=∑j∈Iu∣sim(i,j)∣∑j∈Iusim(i,j)⋅ruj
Both prediction formulas, user-based and item-based, are weighted averages. The derivation shows what a weighted average is the best answer to, and why dividing by the sum of the similarities is not optional.
#Adjusted Cosine: The Trick That Made Item-Based CF Work
Plain cosine similarity over items has a hidden bug: it doesn't account for user rating bias. User A rates everything 4-5; user B rates everything 1-2. Two items that A rates and B rates look identical by cosine, but really, both users are just expressing baseline behavior, not item similarity.
Adjusted cosine subtracts each user's mean before comparing items:
Both the Pearson formula and the adjusted cosine are cosines of vectors with the user's average removed. Open the derivation to see why, and why removing the average is the fix.
You're building a recommender for a site with 10 million users and 100,000 items. Each user has touched ~50 items, each item has ~5,000 ratings. Would you use user-based CF or item-based CF for production?
Now let's actually build user-based CF from scratch on a tiny MovieLens-style dataset, compute cosine similarities, and surface top-N recommendations.
A Pyodide-backed scratchpad for math lessons.
Loading visualization...
Quick check
A new user signs up to a music streaming service, listens to nothing, and immediately taps 'For You'. What does *pure* collaborative filtering predict for them?
#Matrix Factorization: The Idea That Won the Netflix Prize
Tune the latent factors and watch two skinny embedding matrices reconstruct the full user-item ratings table.
If R were dense, we could factor it exactly with the singular value decomposition:
R=UΣV⊤≈UkΣkVk⊤=PQ⊤
Loading visualization...
The SVD viz above lets you decompose a matrix and dial down the rank — watch how a rank-2 or rank-5 approximation still captures most of the structure of a much larger matrix. This is exactly what matrix factorization does to the user-item table: throw away the high-rank noise, keep the low-rank "true taste" signal.
#Why Plain SVD Doesn't Work on Real Rec Data: Funk-SVD
There's a catch. SVD as stated requires R to be fully observed. Real R is 99.99% missing. The classical workaround — fill missing entries with the column mean and SVD that — was the standard approach until 2006. It works terribly: you're factoring noise.
In 2006, Simon Funk (an amateur ML hobbyist) posted a blog entry during the Netflix Prize describing what is now called Funk-SVD (sometimes just "regularized matrix factorization"). The idea: don't bother with the missing entries at all. Just optimize P and Q via gradient descent on the observed entries only:
P,Qmin(u,i)∈Ω∑(rui−pu⋅qi)2+λ(∥pu∥2+∥qi∥2)
This single change — only summing over observed entries plus L2 regularization — turned matrix factorization from "academic curiosity" into "production-grade recommender". Funk-SVD gave a large RMSE improvement over the baseline and became the bedrock of the eventual Netflix Prize winner.
Two things in that block need a derivation: the SGD update in the code, and the Alternating Least Squares step that the next sections use. Note that in this objective the penalty for p_u is counted once for every rating of that user, so a user with many ratings is regularised more. The SGD rule follows directly, and the ALS rule follows once you see that the problem splits by user.
Three recommender families, blended in production: content-based (item features), collaborative filtering (user behavior), and matrix factorization / embeddings (latent factors). Pure approaches lose to hybrids that combine all three.
Item-based CF dominated user-based CF in production because item-item similarity is stable, precomputable offline, and reduces online prediction to a small weighted sum over the user's history.
Matrix factorization is the soul of modern recs: decompose the sparse user-item matrix into low-rank user and item embeddings, predict via dot product. Funk-SVD (regularized MF on observed entries) was the Netflix Prize breakthrough. Part 2 adds the implicit-feedback case that dominates real products.