Recommender systems
Be able to build a simple recommender system with collaborative filtering.
Prerequisites
Intuition
Collaborative filtering: «users like you liked X». It builds on behavioural patterns, not on the content.
Content-based: «this resembles what you liked». It builds on properties of the items.
Matrix factorisation is the standard method for the first: the rating matrix R (users × items) is sparse but approximately low-rank — tastes can be described with a handful of hidden factors. Factorise R ≈ U·Vᵀ where U is the user factors and V the item factors, and fill the gaps in.
Cold start is the system's hardest problem: a new user has no history, a new item no ratings. The solution is hybrid — use content and metadata until behavioural data exists.
Code
import numpy as np
def als(R, mask, k=10, lam=0.1, iterations=20, seed=0):
"""Alternating least squares on a sparse rating matrix."""
rng = np.random.default_rng(seed)
n, m = R.shape
U = rng.normal(0, 0.1, (n, k)); V = rng.normal(0, 0.1, (m, k))
for _ in range(iterations):
for i in range(n): # fix V, solve for U
j = mask[i]
if j.any():
Vj = V[j]
U[i] = np.linalg.solve(Vj.T @ Vj + lam * np.eye(k), Vj.T @ R[i, j])
for j in range(m): # fix U, solve for V
i = mask[:, j]
if i.any():
Ui = U[i]
V[j] = np.linalg.solve(Ui.T @ Ui + lam * np.eye(k), Ui.T @ R[i, j])
return U, V
def recommend(U, V, user, already_seen, n=5):
score = U[user] @ V.T
score[list(already_seen)] = -np.inf
return np.argsort(-score)[:n]
Evaluation: RMSE on ratings measures the wrong thing. The user sees a list, so measure ranking: precision@k, recall@k, nDCG@k, and coverage (how much of the catalogue is ever recommended?).
Two effects to design against:
- Popularity bias — the system recommends what is popular, which makes it more popular. Counteract it with diversification.
- The filter bubble — the user only sees more of the same. Deliberately build in exploration (epsilon-greedy or bandits).
AI-grafen's exercise selection is a variant of the same problem: the bandit algorithm chooses the explanation depth, and the exploration is built in precisely so as not to get stuck.
Mastery means
- Builds collaborative filtering with matrix factorisation
- Handles the cold-start problem
- Evaluates with ranking metrics
Sign in to do the exercises and build your mastery up.