Machine Learning Full Course · Episode 54. Every code sample below was run with Python 3.13, NumPy 2.5.3 and pandas 3.0.5; the output shown is the real output. Meera's tiffin app is the same made-up data as in Episode 53 (500 customers, 60 dishes, 20% of ratings hidden).
The big idea: comparing every pair of users does not scale: at Netflix-Prize size the similarity matrix alone needs 1.82 TiB (MemoryError). Instead, describe every user and every item with a few hidden numbers, latent factors, called embeddings, and predict a rating as their dot product: the rating matrix is factorised into two thin matrices. Learned only from the known ratings with an L2 penalty, by SGD (MAE 0.879 → 0.433 after 30 epochs) or ALS (0.902 → 0.467 in 10 iterations), it beats Episode 53's best collaborative filtering (0.642). Users see a list, so we judge it with precision@k, recall@k, MAP and nDCG: matrix factorisation more than doubles popularity on all four. A hybrid that maps tags to embeddings helps brand-new dishes (0.862 → 0.762). Offline tests choose candidates; an online A/B test chooses the winner.
Sources: Y. Koren, R. Bell and C. Volinsky, "Matrix Factorization Techniques for Recommender Systems", Computer (IEEE), August 2009, pp. 42-49, which names stochastic gradient descent and alternating least squares as the two approaches and describes the Netflix data as more than 100 million ratings from about 500,000 customers on more than 17,000 movies (PDF); the Netflix Prize (started October 2006, $1 million for at least a 10 percent improvement, more than 51,000 contestants from 186 countries, won in 2009 by the seven-member team BellKor's Pragmatic Chaos) (phys.org, 2009); K. Järvelin and J. Kekäläinen, "Cumulated gain-based evaluation of IR techniques", ACM Transactions on Information Systems 20(4):422-446, 2002, which introduced (n)DCG (record).
Words you need first
- User-item matrix, sparsity, collaborative filtering, cold start, content-based filtering (Episode 53).
- Dot product (Episode 3): multiply two vectors pairwise and add: (2, 9) · (0, 10) = 0 + 90 = 90.
- Matrix multiplication, transpose (Episode 4): P (users × k) times Qᵀ (k × items) gives a users × items table; Qᵀ is Q with rows and columns swapped.
- SVD and low-rank approximation (Episode 5): writing a full matrix as a product of thinner ones.
- Gradient descent, learning rate, SGD (Episodes 8, 18): repeatedly step against the gradient; SGD updates after each single example.
- L2 regularisation / ridge regression (Episode 21): add λ × (sum of squared weights) to the loss so weights stay small and the model doesn't memorise.
- Epoch: one full pass over the training data.
- MAE (Episode 19): average size of a miss, here in stars.
The problem: neighbours don't scale
import numpy as np
users, movies = 500_000, 17_000 # Netflix Prize scale (Koren et al., 2009)
print("ratings we have :", f"{100_000_000:,}")
print("cells in matrix :", f"{users * movies:,}", f"-> {100_000_000 / (users * movies):.1%} filled")
S = np.zeros((users, users)) # user-user similarity, like episode 53
print("similarity matrix ready")
Output:
ratings we have : 100,000,000
cells in matrix : 8,500,000,000 -> 1.2% filled
Traceback (most recent call last):
File "main.py", line 5, in <module>
S = np.zeros((users, users))
numpy._core._exceptions._ArrayMemoryError: Unable to allocate 1.82 TiB for an array with shape (500000, 500000) and data type float64
- One similarity per pair of users: 500,000² = 250 billion numbers × 8 bytes = 1.82 TiB (tebibytes; nearly two million megabytes).
- The matrix is about 99% empty, so two random users have rarely rated the same items: their similarity rests on almost nothing.
Latent factors and embeddings: taste dials
Imagine a few taste dials. Asha: spice 2/10, sweetness 9/10. Rasmalai: spice 0/10, sweetness 10/10. Matching dials → a high rating.
- User embedding: a short vector of k numbers describing a user.
- Item embedding: a vector of the same k numbers describing an item.
- Latent factor: one of those k dials. "Latent" means hidden: we never name them; the data decides what they mean.
- Predicted rating ≈ user vector · item vector (dot product).
Matrix factorisation
R (users × items) ≈ P (users × k) · Qᵀ (k × items)
- Matrix factorisation: writing one big matrix as the product of two thin ones, like 12 = 3 × 4.
- Storage with k = 50: (500,000 + 17,000) × 50 = 25.85 million numbers instead of 8.5 billion cells.
- Why not plain SVD (Episode 5)? SVD needs every cell; filling the 99% empty cells with 0 would tell the model everyone hated everything they haven't seen. So we learn only from the known cells.
The full guess, with biases:
guess = μ + b_user + b_dish + p_user · q_dish
- μ: the overall average rating.
- User bias b_user: this user rates a bit high or low (a generous rater).
- Item bias b_dish: this dish gets a bit more or less from everyone (a crowd-pleaser).
The loss, over known ratings only:
loss = Σ (stars − guess)² + λ (|p|² + |q|² + b_user² + b_dish²)
Koren, Bell and Volinsky (2009) name two ways to minimise it: stochastic gradient descent and alternating least squares.
Learning with SGD
For each known rating, in random order: error e = stars − guess; then nudge every parameter against the gradient:
b_user += lr × (e − λ b_user),b_dish += lr × (e − λ b_dish)p_user += lr × (e q_dish − λ p_user),q_dish += lr × (e p_user − λ q_dish)
These lines are BIG (the same data as Episode 53):
import numpy as np, pandas as pd; r = np.random.default_rng(53) # Meera's tiffin app, the same made-up data as ep 53
U, I = 500, 60; F, P = r.normal(0, 1, (I, 2)), r.normal(0, 1, (U, 2)) # 500 customers, 60 dishes (0-59), hidden tastes
tags = pd.DataFrame(np.c_[F + r.normal(0, .7, (I, 2)), r.normal(0, 1, (I, 2))] > 0, columns="spicy veg sweet south".split())
true = 3 + r.normal(0, 0.3, (U, 1)) + r.normal(0, 0.4, (1, I)) + 0.9 * P @ F.T
R = pd.DataFrame([(u, j, int(np.clip(round(true[u, j] + r.normal(0, 0.3)), 1, 5))) for u in range(U)
for j in r.choice(I, r.poisson(12) + 1, replace=False)], columns=["user", "dish", "stars"])
test = R.sample(frac=0.2, random_state=0); train = R.drop(test.index); mu = train.stars.mean() # hide 20%; average
# BIG, then:
mae = lambda p: np.abs(np.clip(p, 1, 5) - test.stars.values).mean().round(3) # average miss on the hidden ratings
k, lr, lam = 2, 0.02, 0.05; P, Q = r.normal(0, 0.1, (U, k)), r.normal(0, 0.1, (I, k)) # embeddings start random
bu, bi = np.zeros(U), np.zeros(I) # user and dish biases
data = list(train.itertuples(index=False)); pred = lambda u, j: mu + bu[u] + bi[j] + P[u] @ Q[j]
for epoch in range(1, 31):
for n in r.permutation(len(data)): # SGD: one rating at a time
u, j, s = data[n]; e = s - pred(u, j)
bu[u] += lr * (e - lam * bu[u]); bi[j] += lr * (e - lam * bi[j])
P[u], Q[j] = P[u] + lr * (e * Q[j] - lam * P[u]), Q[j] + lr * (e * P[u] - lam * Q[j])
if epoch in (1, 5, 10, 30): print(f"epoch {epoch:2}: MAE", mae([pred(u, j) for u, j in zip(test.user, test.dish)]))
Output:
epoch 1: MAE 0.879
epoch 5: MAE 0.874
epoch 10: MAE 0.799
epoch 30: MAE 0.433
The vectors start tiny and random, so for a few epochs the model is no better than the average; then they "lock in" to the hidden tastes: 0.433 after 30 epochs (Episode 53's item-based CF: 0.642).
Alternating least squares (ALS)
Finding P and Q together is hard because they multiply each other. But:
- Freeze Q: each user's vector is a ridge regression of that user's ratings on the vectors of the dishes they rated, with an exact solution
(AᵀA + λI)⁻¹ Aᵀb. - Freeze P: each dish's vector is a ridge regression the same way.
- Alternate. Each user (and each dish) is solved independently, so it parallelises across machines, and there is no learning rate.
# BIG, then:
mae = lambda p: np.abs(np.clip(p, 1, 5) - test.stars.values).mean().round(3) # average miss on the hidden ratings
k, lam = 3, 2.0; uu, jj = train.user.values, train.dish.values; y = train.stars.values - mu # stars minus the average
P = r.normal(0, 0.1, (U, k)); Q = r.normal(0, 0.1, (I, k)) # user and dish embeddings
ridge = lambda A, b: np.linalg.solve(A.T @ A + lam * np.eye(k), A.T @ b) # least squares + L2 penalty
tu, tj = test.user.values, test.dish.values
for it in range(1, 11): # ALS: alternate between two easy problems
for u in range(U): m = uu == u; P[u] = ridge(Q[jj[m]], y[m]) # dishes fixed -> best user vectors
for j in range(I): m = jj == j; Q[j] = ridge(P[uu[m]], y[m]) # users fixed -> best dish vectors
if it in (1, 2, 5, 10): print(f"iteration {it:2}: MAE", mae(mu + (P[tu] * Q[tj]).sum(1)))
Output:
iteration 1: MAE 0.902
iteration 2: MAE 0.733
iteration 5: MAE 0.489
iteration 10: MAE 0.467
(Here the ratings are centred on μ and no separate biases are used; k = 3.)
| Method | MAE (stars) |
|---|---|
| Item-based CF (Episode 53) | 0.642 |
| ALS, k = 3, 10 iterations | 0.467 |
| SGD, k = 2 + biases, 30 epochs | 0.433 |
The Netflix Prize
In October 2006 Netflix offered $1 million to anyone who could predict ratings at least 10% better than its own system. More than 51,000 contestants from 186 countries took part; in 2009 the seven-member team BellKor's Pragmatic Chaos won. Matrix factorisation techniques were central to the competition (Koren, Bell and Volinsky, 2009).
Ranking metrics: users see a list
The app shows a top k list. A wrong guess for a dish at position 40 doesn't matter; a bad dish at position 1 does. A relevant item is one the user really liked (4-5 stars in the hidden test set).
| Metric | Meaning | Formula |
|---|---|---|
| precision@k | share of the top k that is relevant | hits in top k / k |
| recall@k | share of all relevant items found in the top k | hits in top k / number of relevant items |
| AP@k (average precision) | rewards hits near the top | sum of precision@i at each hit position i / min(relevant, k) |
| MAP@k | mean average precision | mean of AP@k over users |
| DCG@k | discounted cumulative gain | Σ hit_i / log2(i + 1) |
| nDCG@k | normalised DCG, 1.0 = perfect order | DCG / best possible DCG |
nDCG comes from Järvelin and Kekäläinen (2002).
import numpy as np
top5 = ["Paneer Tikka", "Rasmalai", "Rajma Chawal", "Chicken Biryani", "Idli Sambar"] # the app's list for Asha
liked = {"Rajma Chawal", "Paneer Tikka", "Masala Dosa"} # what she really loved (hidden test set)
hit = np.array([d in liked for d in top5]) # [1 0 1 0 0]
print("hits:", hit.astype(int))
print("precision@5:", hit.mean()) # share of the list that was good
print("recall@5:", round(hit.sum() / len(liked), 3)) # share of her favourites we found
print("AP@5:", round(sum(hit[:i + 1].mean() for i in range(5) if hit[i]) / len(liked), 3)) # precision at each hit
dcg = (hit / np.log2(np.arange(2, 7))).sum(); ideal = (1 / np.log2(np.arange(2, 5))).sum()
print("nDCG@5:", round(dcg / ideal, 3)) # top positions count more
Output:
hits: [1 0 1 0 0]
precision@5: 0.4
recall@5: 0.667
AP@5: 0.556
nDCG@5: 0.704
By hand: hits at positions 1 and 3. precision@5 = 2/5 = 0.4. recall@5 = 2/3 = 0.667. AP@5 = (1/1 + 2/3) / 3 = 0.556. DCG = 1/log2(2) + 1/log2(4) = 1 + 0.5 = 1.5; best DCG with 3 relevant = 1 + 0.631 + 0.5 = 2.131; nDCG = 1.5 / 2.131 = 0.704.
All 500 customers
# BIG + ALS lines (below), then:
G = np.zeros((U, I), bool); g = test[test.stars >= 4]; G[g.user, g.dish] = True # each user's hidden favourites
n = G.sum(1); ok = n > 0; m = np.minimum(n, 5)[ok]; disc = 1 / np.log2(np.arange(2, 7)) # nDCG discount, positions 1-5
for name, S in [("popularity", np.tile(train.groupby("dish").stars.mean().values, (U, 1))), ("matrix fac", P @ Q.T)]:
S[train.user, train.dish] = -np.inf; h = G[np.arange(U)[:, None], np.argsort(-S, 1)[:, :5]][ok] # hits in top 5
rc, ap, nd = h.sum(1) / n[ok], (h.cumsum(1) / np.arange(1, 6) * h).sum(1) / m, (h * disc).sum(1) / disc.cumsum()[m - 1]
print(f"{name:10} precision {h.mean():.3f} recall {rc.mean():.3f} MAP {ap.mean():.3f} nDCG {nd.mean():.3f}")
The ALS lines:
k, lam = 3, 2.0; uu, jj = train.user.values, train.dish.values; y = train.stars.values - mu
P, Q = r.normal(0, .1, (U, k)), r.normal(0, .1, (I, k)); ridge = lambda A, b: np.linalg.solve(A.T@A + lam*np.eye(k), A.T@b)
for it in range(10): # ALS from the last program
for u in range(U): m = uu == u; P[u] = ridge(Q[jj[m]], y[m])
for j in range(I): m = jj == j; Q[j] = ridge(P[uu[m]], y[m])
Output:
popularity precision 0.041 recall 0.119 MAP 0.057 nDCG 0.084
matrix fac precision 0.089 recall 0.293 MAP 0.157 nDCG 0.208
- Popularity here = the best-rated dishes overall, the same list for everyone.
- Matrix factorisation more than doubles every metric (recall@5 0.120 → 0.293, nDCG@5 0.085 → 0.208).
- The numbers look small because each customer has only 2-3 hidden ratings among ~50 unrated dishes. Compare models; don't expect 1.0.
Hybrid recommenders: new dishes
A new dish has no ratings, so its embedding is never learned. Its tags are known, so learn a mapping tags → embedding on dishes that have ratings (a linear regression), and use it for new dishes. Combining content and collaborative information like this is a hybrid recommender; other hybrids switch between methods or blend their scores.
# BIG, then:
cold = np.arange(6); train = train[~train.dish.isin(cold)] # dishes 0-5 are new: no ratings yet
k, lam = 3, 2.0; uu, jj = train.user.values, train.dish.values; y = train.stars.values - mu
P, Q = r.normal(0, .1, (U, k)), r.normal(0, .1, (I, k)); ridge = lambda A, b: np.linalg.solve(A.T@A + lam*np.eye(k), A.T@b)
for it in range(10): # ALS from the last program
for u in range(U): m = uu == u; P[u] = ridge(Q[jj[m]], y[m])
for j in range(I): m = jj == j; Q[j] = ridge(P[uu[m]], y[m])
warm = ~np.isin(range(I), cold); T = np.c_[tags.values, np.ones(I)]; W = np.linalg.lstsq(T[warm], Q[warm], rcond=None)[0]
t = test[test.dish.isin(cold)]; tu, tj = t.user.values, t.dish.values
for name, Qc in [("MF only (no ratings)", np.zeros((I, k))), ("hybrid: tags -> embedding", T @ W)]:
print(f"{name:26} MAE on new dishes:", np.abs(np.clip(mu + (P[tu] * Qc[tj]).sum(1), 1, 5) - t.stars).mean().round(3))
(COLD is the first line; the ALS lines follow it.) Output:
MF only (no ratings) MAE on new dishes: 0.862
hybrid: tags -> embedding MAE on new dishes: 0.762
Offline vs online evaluation
| Offline evaluation | Online evaluation | |
|---|---|---|
| What | test on past, logged data (everything in this episode) | test with real users: an A/B test, users randomly split, half see A, half see B |
| Measures | MAE, precision@k, nDCG | orders, re-orders, clicks, time to find something |
| Good | fast, cheap, safe, repeatable | measures what the business really cares about |
| Limit | the past only shows reactions to what the old system showed | slow, needs traffic, a bad model affects real users |
Offline picks the candidates; online picks the winner.
Common mistakes
| Mistake | Exact message / symptom | Why | Fix |
|---|---|---|---|
| All-pairs similarity at scale | MemoryError: Unable to allocate 1.82 TiB for an array with shape (500000, 500000) and data type float64 |
grows with users² | embeddings (matrix factorisation) |
| Filling empty cells with 0 before SVD | predictions pulled towards 0 stars | unknown ≠ hated | loss on known cells only |
| No L2 penalty at all | ridge matrix AᵀA can be singular for a user with fewer ratings than k | nothing keeps vectors small | λ > 0, tuned on validation data |
| Too strong a penalty | λ = 20: MAE 0.804 | vectors shrink to almost 0 | tune λ |
| Judging a top-k list by MAE | a good MAE can still put bad items first | order matters | precision@k, recall@k, MAP, nDCG |
| Shipping on offline scores | offline winner loses with real users | logged data is biased | A/B test |
Mini project: blend MF with content
Blend the ALS prediction with a content-based score (user tag profile · dish tags, turned into stars by a small regression).
# BIG + ALS lines, then:
mae = lambda p: np.abs(np.clip(p, 1, 5) - test.stars.values).mean().round(3)
likes = train[train.stars >= 4].merge(tags, left_on="dish", right_index=True).groupby("user")[list(tags.columns)].mean()
prof = likes.reindex(range(U)).fillna(0).values # content: each user's tag profile
tu, tj = test.user.values, test.dish.values; T = tags.values.astype(float)
mf = mu + (P[tu] * Q[tj]).sum(1) # matrix factorisation guess
X = np.c_[(prof[tu] * T[tj]).sum(1), np.ones(len(tu))] # content score: profile · tags
Xt = np.c_[(prof[uu] * T[jj]).sum(1), np.ones(len(uu))]
w = np.linalg.lstsq(Xt, train.stars.values, rcond=None)[0]; content = X @ w # turn the score into stars
for a in [0, 0.5, 0.8, 1]: print(f"{a:.0%} MF + {1 - a:.0%} content: MAE", mae(a * mf + (1 - a) * content))
Output:
0% MF + 100% content: MAE 0.905
50% MF + 50% content: MAE 0.631
80% MF + 20% content: MAE 0.511
100% MF + 0% content: MAE 0.467
On dishes with ratings, MF alone is best; content helps only where MF has nothing (the new dishes above). Choose blending weights on a validation set, not the test set.
Practice
1. SGD with k = 1, 2, 5 and 20 latent factors.
Solution
# BIG, then:
mae = lambda p: np.abs(np.clip(p, 1, 5) - test.stars.values).mean().round(3)
data = list(train.itertuples(index=False))
def sgd(k, lr=0.02, lam=0.05, epochs=30):
P = r.normal(0, 0.1, (U, k)); Q = r.normal(0, 0.1, (I, k)); bu, bi = np.zeros(U), np.zeros(I)
for epoch in range(epochs):
for n in r.permutation(len(data)):
u, j, s = data[n]; e = s - (mu + bu[u] + bi[j] + P[u] @ Q[j])
bu[u] += lr * (e - lam * bu[u]); bi[j] += lr * (e - lam * bi[j])
P[u], Q[j] = P[u] + lr * (e * Q[j] - lam * P[u]), Q[j] + lr * (e * P[u] - lam * Q[j])
return mae([mu + bu[u] + bi[j] + P[u] @ Q[j] for u, j in zip(test.user, test.dish)])
for k in [1, 2, 5, 20]: print(f"k = {k:2}: MAE", sgd(k))
k = 1: MAE 0.765
k = 2: MAE 0.429
k = 5: MAE 0.457
k = 20: MAE 0.464
One factor is too few. Our made-up data has 2 hidden tastes, so k = 2 does best; more factors start fitting noise.
2. ALS with λ = 0.1, 2 and 20.
Solution
# BIG, then:
mae = lambda p: np.abs(np.clip(p, 1, 5) - test.stars.values).mean().round(3)
k = 3; uu, jj = train.user.values, train.dish.values; y = train.stars.values - mu; tu, tj = test.user.values, test.dish.values
for lam in [0.1, 2.0, 20.0]:
P, Q = r.normal(0, 0.1, (U, k)), r.normal(0, 0.1, (I, k)); ridge = lambda A, b: np.linalg.solve(A.T@A + lam*np.eye(k), A.T@b)
for it in range(10):
for u in range(U): m = uu == u; P[u] = ridge(Q[jj[m]], y[m])
for j in range(I): m = jj == j; Q[j] = ridge(P[uu[m]], y[m])
print(f"lam = {lam:4}: MAE after 10 iterations", mae(mu + (P[tu] * Q[tj]).sum(1)))
lam = 0.1: MAE after 10 iterations 0.473
lam = 2.0: MAE after 10 iterations 0.489
lam = 20.0: MAE after 10 iterations 0.804
λ = 0.1 and λ = 2 end up close here (each run also starts from different random vectors); λ = 20 shrinks every vector towards 0 and the model falls back to the average. Tune λ on a validation set.
3. precision@10, recall@10, MAP@10 and nDCG@10.
Solution
In the ranking program, change every 5 to 10 ([:, :10], np.minimum(n, 10), np.arange(2, 12), np.arange(1, 11)):
popularity precision 0.043 recall 0.251 MAP 0.078 nDCG 0.132
matrix fac precision 0.075 recall 0.497 MAP 0.187 nDCG 0.278
Recall rises with k (more room to find favourites); precision falls.
Quick quiz
The app shows A, B, C, D; the user liked B, D, F, G and H. precision@4 and recall@4?
Answer
0.5 and 0.4. Hits: B and D. precision@4 = 2/4 = 0.5; recall@4 = 2 of 5 = 0.4.
More questions:
- Why is matrix factorisation cheaper to store than item-item similarities for 17,000 items?
Answer
17,000 × k numbers (850,000 for k = 50) instead of 17,000² ≈ 289 million similarities. - In ALS, why is each step easy?
Answer
With one side frozen, each user's (or item's) vector is a ridge regression with an exact solution. - Two lists have the same hits, one at positions 1-2, the other at 4-5. Which metrics differ?
Answer
precision@5 and recall@5 are equal; AP and nDCG are higher for the list with hits at the top. - What do you do with a new dish in a pure MF recommender?
Answer
Its embedding is unknown; use a hybrid (tags → embedding), content-based scores or a popularity slot until ratings arrive.
Interview questions
- Explain matrix factorisation for recommendations. Learn a k-dimensional vector per user and per item so their dot product (plus biases) approximates the known ratings; minimise squared error on known entries with L2 regularisation; predict missing entries.
- SGD vs ALS? SGD: simple, updates per rating, needs a learning rate. ALS: alternates exact ridge solutions, no learning rate, parallelises well and suits implicit feedback where all cells are considered.
- Why include user and item biases? Much of the variation is "this user rates high" or "this item is liked by everyone"; biases capture it so the factors can model real taste interactions.
- precision@k vs recall@k vs nDCG? Precision: share of shown items that are relevant; recall: share of relevant items shown; nDCG: rewards relevant items near the top, normalised to 1 for a perfect order.
- What is a hybrid recommender? One that combines content features and collaborative signals: switching, weighted blending, or feeding features into the model (e.g. mapping item tags to embeddings) to handle cold start.
- Offline vs online evaluation? Offline metrics on logged data are cheap but biased by what the old system showed; online A/B tests measure real business outcomes and decide.
Glossary
| Term | Meaning (plain words + example) |
|---|---|
| Latent factor | A hidden "taste dial" learned from the data, never named by us |
| Embedding | A short vector of numbers describing something: a user or an item |
| User embedding | The user's k numbers: Asha (2, 9) |
| Item embedding | The item's k numbers: Rasmalai (0, 10) |
| Matrix factorisation | Writing the rating matrix as P · Qᵀ, two thin matrices |
| k (number of factors) | How many dials each embedding has: 2, 3, 50 |
| Bias (user / item) | An offset: a generous rater, a crowd-pleaser dish |
| μ (global mean) | The average of all known ratings |
| L2 penalty, λ | λ × sum of squared parameters added to the loss; keeps vectors small |
| Stochastic gradient descent (SGD) | Update the parameters after each single rating, against the error's gradient |
| Learning rate | Size of each SGD step: 0.02 |
| Epoch | One full pass over all training ratings |
| Alternating least squares (ALS) | Freeze items, solve users exactly; freeze users, solve items; repeat |
| Ridge regression | Least squares with an L2 penalty: solve (AᵀA + λI) w = Aᵀb |
| Hybrid recommender | Combines content-based and collaborative methods |
| Relevant item | An item the user really liked: 4-5 stars in the hidden test set |
| Top-k list | The k items shown first |
| precision@k | Hits in the top k ÷ k |
| recall@k | Hits in the top k ÷ all relevant items |
| Average precision (AP@k) | Average of precision at each hit position (÷ min(relevant, k)) |
| MAP (mean average precision) | AP@k averaged over users |
| DCG | Discounted cumulative gain: Σ hit_i / log2(i + 1) |
| nDCG | DCG ÷ the best possible DCG; 1.0 = perfect order |
| Offline evaluation | Testing on past, logged data |
| Online evaluation | Testing with live users |
| A/B test | Randomly split users; group A sees one version, group B another; compare real outcomes |
| Netflix Prize | 2006-2009 contest: $1 million for a 10% better rating predictor |
| MemoryError | Python's error when an array can't fit in memory |
| TiB | Tebibyte, 2⁴⁰ bytes ≈ 1.1 trillion bytes |
Summary
- Neighbour methods need all pairs: 1.82 TiB at Netflix-Prize scale. Embeddings need a few numbers per user and item.
- Matrix factorisation: rating ≈ μ + biases + user vector · item vector, learned on known ratings with an L2 penalty.
- SGD: 0.433 after 30 epochs; ALS: 0.467 in 10 iterations (CF in Episode 53: 0.642).
- Judge lists with precision@k, recall@k, MAP, nDCG: MF more than doubled popularity on all four.
- Hybrid (tags → embedding) for new dishes: 0.862 → 0.762.
- Offline evaluation chooses candidates; an online A/B test chooses the winner.
Next episode: Reinforcement learning basics: agent, environment, state, action, reward, policy, Q-learning and multi-armed bandits.