Skip to content
Lambdian

Matrix factorisation and recommenders

Low-rank models of user–item matrices: SVD, latent factors, ALS and collaborative filtering.

Animated explanation

The idea

Netflix knows a few of your ratings and a few of millions of other people's. How can it guess how you'd rate a film you haven't seen? Assume tastes are driven by a handful of hidden factors: how much you like action, romance, documentaries… Each film has the same kind of profile. Your predicted rating is how well your taste vector matches the film's profile: a dot product.

Learning those vectors from the known ratings is matrix factorisation: approximate the huge, mostly empty rating matrix RR by UV⊤UV^\top with only kk columns each. It is unsupervised structure discovery (the factors are never labelled) put to work for prediction. The same low-rank idea appears in topic models (NMF), image compression (truncated SVD) and word embeddings.

SVD and the missing-data problem

For a complete matrix, the truncated SVD gives the best rank-kk approximation (Eckart–Young). Rating matrices are mostly missing, and treating gaps as zeros would teach the model that unseen means disliked. So we fit the factors only on observed entries, with L2 regularisation to avoid overfitting the few ratings each user has, using SGD or alternating least squares.

Practical recommender details

Add user and item biases (some users rate everything high; some films are universally loved). Handle implicit feedback (clicks, plays) with weighted ALS. Cold start: new users or items have no ratings, so blend in content features. Evaluate with RMSE on held-out ratings, but for products also with ranking metrics (precision@k, NDCG).

The algorithm, step by step

  1. 1
    Collect observed triples (u,i,rui)(u, i, r_{ui}); hold some out for validation.
  2. 2
    Initialise user factors PP and item factors QQ with small random values.
  3. 3
    ALS: alternately solve a ridge regression for each user's pu\mathbf{p}_u (Q fixed) and each item's qi\mathbf{q}_i (P fixed). Or SGD over shuffled ratings.
  4. 4
    Stop when validation RMSE stops improving; recommend the unseen items with the highest pu⊤qi\mathbf{p}_u^\top\mathbf{q}_i.

The maths

Model

r^ui=pu⊤qi\hat r_{ui} = \mathbf{p}_u^\top\mathbf{q}_i

Regularised objective

∑(u,i)∈Ω(rui−pu⊤qi)2+λ(∑u∥pu∥2+∑i∥qi∥2)\sum_{(u,i)\in\Omega}(r_{ui}-\mathbf{p}_u^\top\mathbf{q}_i)^2 + \lambda\Big(\sum_u\lVert\mathbf{p}_u\rVert^2 + \sum_i\lVert\mathbf{q}_i\rVert^2\Big)

ALS update for one user

pu=(QΩu⊤QΩu+λI)−1QΩu⊤rΩu\mathbf{p}_u = (Q_{\Omega_u}^\top Q_{\Omega_u} + \lambda I)^{-1}Q_{\Omega_u}^\top\mathbf{r}_{\Omega_u}

Truncated SVD

Ak=UkSkVk⊤A_k = U_kS_kV_k^\top

Strengths and weaknesses

Strengths

  • Captures hidden structure from sparse data
  • Scales to millions of users and items (ALS parallelises)
  • Factors double as user and item embeddings

Weaknesses

  • Cold start for new users and items
  • Factors are not directly interpretable
  • Needs regularisation and tuning of k and λ

Reach for it when…

Collaborative filtering and any large, sparse matrix of interactions or co-occurrences.

Check your understanding

Question 1

Q1Why not just fill missing ratings with 0 and run SVD?

Question 2

Q2In ALS, with item factors fixed, updating one user's factors is…

Question 3

Q3Eckart–Young says the best rank-k approximation of a complete matrix is…

Lambdian Lab

ALS for a rating matrix

Ratings come as a dense matrix R with a boolean mask M of observed cells.

  • rank_k(A, k): the best rank-k approximation of a complete matrix via SVD.
  • als(R, M, k, lam, iters, seed): alternating least squares on observed cells; return (P, Q) with predictions P @ Q.T.
  • rmse(R, M, P, Q): root mean squared error on the cells where M is True.
Open the labPython + NumPy + scikit-learn runs in your browser. Sign-in keeps your code and attempts.