Artificial Intelligence & Data Science is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Recommender systems

Clear, exam-focused notes on recommender systems: definitions, similarity measures, user/item collaborative filtering, matrix factorization, formulas, worked examples, common mistakes and exam guidance.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Recommender systems help engineers personalise content, products or search results for users — increasing user engagement, conversion and satisfaction. An engineer uses these ideas when building features like product recommendations, movie suggestions, or ranked lists returned by a service.

Key ideas

Start from first principles and simple definitions.

  • User — an entity (person, account) that consumes items. Defined before using.
  • Item — an entity (product, movie, document) that can be recommended.
  • Rating — a measured preference from a user for an item; numeric (for example 1–5). Ratings are unitless (rating-scale units).
  • Interaction — any observable user action with an item (rating, click, purchase). Defined before use.
  • Recommendation problem — given past interactions, predict items a target user will like that they have not yet interacted with.

Principal approaches (each defined before use):

  • Content-based filtering: recommend items similar to those a user liked, using item features (e.g., genre, keywords). Assumes user preferences are captured by features and items have feature vectors.

  • Collaborative filtering (CF): recommend items using patterns of interactions across users and items, without requiring item features. Assumes similar users behave similarly, or similar items receive similar ratings.

    • User-based collaborative filtering: find users similar to target user then aggregate their ratings to predict missing ratings for the target user.

    • Item-based collaborative filtering: find items similar to a target item (based on users' ratings) and use the target user's ratings on similar items to predict.

  • Matrix factorization (a model-based CF technique): approximate the user–item rating matrix R (size users × items) as a product of low-rank latent factor matrices U (users × k) and V (items × k). Assumes user preferences and item properties lie in a low-dimensional latent space.

  • Hybrid systems: combine content-based and collaborative signals to leverage strengths of both.

Important definitions and assumptions used in commonly-tested derivations:

  1. Similarity measure: a function s(a,b) that quantifies closeness between two entities a and b. Two common measures:

    • Cosine similarity (for vectors): measures cosine of angle between vectors in k-dimensional space. Assumes vectors are non-zero and numeric.
    • Pearson correlation (for co-rated items): measures linear correlation between centered rating vectors. Assumes at least two co-rated items.
  2. Prediction in user-based CF (weighted average): the standard formula used in many exams is prediction by weighted sum of neighbors' deviations from their mean (to handle user bias). Assumes known neighbor set N(u) and similarity weights w(u,v).

  3. Matrix factorization objective: minimize squared error on observed ratings plus regularization. This is a convex problem in either U or V when the other is fixed, but not convex jointly. Typical optimization via alternating least squares (ALS) or stochastic gradient descent (SGD).

Diagrams to draw for exams (describe what to draw and label):

  • For user-based CF: draw a small matrix with rows = users and columns = items. Fill some entries with ratings and blanks for missing. Label target user row and show neighbor rows used to predict a blank. Draw arrows from neighbors' ratings to the predicted cell, labeled with their similarity weights.
  • For matrix factorization: draw R ≈ U·V^T. Draw R as a matrix, and show U (users × k) and V (items × k) whose product approximates R. Indicate k (latent dimensions).

Formulas

cosine(u,v) = (r_u · r_v) / (||r_u|| · ||r_v||)

  • r_u, r_v: rating vectors for users u and v restricted to items both rated (unitless); · is dot product; || || is Euclidean norm; applies when comparing two numeric vectors. Adjusted cosine (item-based): for items i and j, center each user's ratings by that user's mean and compute cosine over the users who rated both: sim_adjcos(i,j) = (sum_{u in C} (r_{u,i}-r̄_u)(r_{u,j}-r̄_u)) / ( sqrt(sum_{u in C} (r_{u,i}-r̄_u)^2) · sqrt(sum_{u in C} (r_{u,j}-r̄_u)^2) ).

pearson(u,v) = ( sum_{i in C} (r_{u,i}-r̄_u)(r_{v,i}-r̄_v) ) / ( sqrt( sum_{i in C} (r_{u,i}-r̄_u)^2 ) · sqrt( sum_{i in C} (r_{v,i}-r̄_v)^2 ) )

  • C: set of co-rated items; r_{u,i}: rating by user u for item i; r̄_u: mean rating of user u over C (the co-rated set); unitless; requires |C| ≥ 2 (|C| ≥ 3 preferred) for a meaningful estimate.

r̂_{u,i} = r̄_u + (sum_{v in N(u)} w_{u,v} (r_{v,i} - r̄_v)) / (sum_{v in N(u)} |w_{u,v}|)

  • Item-based CF prediction: r̂_{u,i} = r̄_i + (sum_{j in N(i)} w_{i,j} (r_{u,j} - r̄_j)) / (sum_{j in N(i)} |w_{i,j}|), where N(i) are similar items j that user u has rated.
  • r̂_{u,i}: predicted rating for user u on item i (unitless); r̄u, r̄_v: user mean ratings; w{u,v}: similarity weight between users u and v (unitless); N(u): neighbor set (often top-k neighbors with positive similarity); denominator uses sum of absolute weights to avoid scale issues. Applies in user-based CF using deviations from means.

R ≈ U · V^T with objective

min_{U,V} sum_{(u,i) in K} (r_{u,i} - u_u^T v_i)^2 + λ( ||U||_F^2 + ||V||_F^2 )

  • K: set of observed (user,item) pairs; r_{u,i}: observed rating (unitless); u_u: k-dim user latent vector; v_i: k-dim item latent vector; λ: regularization parameter (unitless); ||·||_F: Frobenius norm. Optimized by ALS or SGD. Also common: add global mean and bias terms, r̂_{u,i} = μ + b_u + b_i + u_u^T v_i, and augment the objective with λ_b(∑_u b_u^2 + ∑_i b_i^2). SGD then also updates b_u := b_u + η( e - λ_b b_u ) and b_i := b_i + η( e - λ_b b_i ).

SGD update (for a single observed pair (u,i)):

e = r_{u,i} - u_u^T v_i u_u := u_u + η( e v_i - λ u_u ) v_i := v_i + η( e u_u - λ v_i )

  • η: learning rate (unitless); e: prediction error (unitless). Applies per-observed-rating update in SGD.

Worked examples

Example 1 — User-based CF numeric (typical exam question). Given: rating scale 1–5. Users U1, U2, U3; Items I1, I2, I3.

Ratings table (blank means unknown): U1: I1=5, I2=3, I3= ? U2: I1=4, I2=2, I3=3 U3: I1=1, I2=5, I3=4

Predict r̂_{U1,I3} using user-based CF with cosine similarity, neighbors = U2 and U3.

Step 1: form rating vectors restricted to co-rated items with U1.

  • For U2: co-rated with U1 are I1 and I2 => r_{U1} = [5,3], r_{U2} = [4,2].
  • For U3: co-rated with U1 are I1 and I2 => r_{U3} = [1,5].

Step 2: compute cosine similarities.

  • cosine(U1,U2) = (5*4 + 3*2) / (sqrt(5^2+3^2)*sqrt(4^2+2^2)) = (20+6)/(sqrt(25+9)*sqrt(16+4)) = 26/(sqrt(34)*sqrt(20)). Compute numerically: sqrt(34)=5.830951894, sqrt(20)=4.472135955. Product=sqrt(680)=26.07680962. So similarity ≈ 26/26.0768 = 0.9971.

  • cosine(U1,U3) = (5*1 + 3*5) / (sqrt(34)*sqrt(1+25)) = (5+15)/(5.83095*5.09902) = 20/29.744 = 0.6726.

Step 3: use weighted deviations from means (compute means over each user's rated items).

  • r̄_{U1} = (5+3)/2 = 4.0
  • r̄_{U2} = (4+2+3)/3 = 3.0
  • r̄_{U3} = (1+5+4)/3 ≈ 3.3333

Step 4: compute prediction using formula.

  • Numerator = w_{1,2}(r_{2,3}-r̄_{2}) + w_{1,3}(r_{3,3}-r̄_{3}) = 0.9971*(3-3.0) + 0.6726*(4-3.3333) = 0 + 0.67260.6667 = 0.67260.6667 ≈ 0.4484.
  • Denominator = |0.9971| + |0.6726| = 1.6697.
  • Deviation = 0.4484 / 1.6697 ≈ 0.2685.
  • Predicted rating r̂_{U1,I3} = r̄_{U1} + deviation = 4.0 + 0.2682 = 4.27 (unitless, round per exam instructions).

Show final answer in bold: 4.27.

Comment: numbers rounded at each step; an examiner expects correct process and reasonable rounding.

Example 2 — Matrix factorization small SGD step (model answer style). Given small observed rating r_{1,1} = 5, k=2, initial u_1 = [0.5, 0.5], v_1 = [0.5, 0.5], λ = 0.02, η = 0.01.

Step 1: compute current prediction = u_1^T v_1 = 0.50.5 + 0.50.5 = 0.5. Step 2: error e = 5 - 0.5 = 4.5. Step 3: update u_1 := u_1 + η( e v_1 - λ u_1 )

  • e v_1 = 4.5 * [0.5,0.5] = [2.25,2.25]
  • λ u_1 = 0.02 * [0.5,0.5] = [0.01,0.01]
  • update term = 0.01 * ([2.25,2.25] - [0.01,0.01]) = 0.01 * [2.24,2.24] = [0.0224,0.0224]
  • new u_1 = [0.5,0.5] + [0.0224,0.0224] = [0.5224,0.5224]. Step 4: update v_1 similarly (using the pre-update u_1 and v_1 values in this simultaneous step) => new v_1 = [0.5224,0.5224].

Final: a single SGD step moves both latent vectors slightly towards explaining the large positive error.

Common mistakes

  • Not normalising similarity weights (missing denominator) which leads to scale mismatch.
  • Confusing user-based and item-based algorithms: they are symmetric conceptually but have different practical performance at scale.
  • In matrix factorization, forgetting regularisation (λ) leads to overfitting; using too large η (learning rate) can diverge.
  • Not checking the minimum number of co-rated items before computing Pearson correlation (needs at least 2 for a meaningful correlation).

For your exam

Types of questions you will see:

  • Definitions: define collaborative filtering, content-based, similarity measures, matrix factorization.
  • Short notes: explain user-based vs item-based CF, advantages and disadvantages, scalability issues, and challenges like cold-start (new user/item), sparsity, popularity bias, and diversity/novelty.
  • Derivations: show steps to get the prediction formula r̂_{u,i} using weighted neighbor deviations; derive SGD update from the squared-error objective with L2 regularisation.
  • Numericals: compute cosine or Pearson similarity for small rating vectors; predict a rating using user/item CF; perform one SGD update step in matrix factorization.
  • Evaluation: compute and interpret RMSE/MAE for rating prediction and Precision@k/Recall@k/NDCG@k for top-k recommendation; describe offline train/validation/test splits.
  • Diagrams: draw rating matrix and label neighbors, or show factorisation R ≈ U·V^T.

What a full-marks answer includes:

  • Clear definitions of all terms used (user, item, rating, similarity).
  • Statement of assumptions (e.g., which items used to compute similarity, whether means are over co-rated items or all items).
  • Step-by-step derivation for formulas requested, with algebraic steps shown.
  • Numerical answers with intermediate steps, correct rounding and units (unitless ratings).
  • For algorithms, brief note on complexity/scalability: computing a full user–user similarity matrix is O(#users^2·#items) (and item–item is O(#items^2·#users)); item-based often scales better when #items << #users; matrix factorization training cost is O(|K|·k·epochs), where |K| is the number of observed ratings.

Related topics (do not re-teach here): TF-IDF and the inverted index, Vector space model and cosine ranking, Efficient scoring and top-k retrieval, Parallel information retrieval and MapReduce.

Quick check

  1. Define item-based collaborative filtering in one sentence.
  2. Which similarity measure uses mean-centred ratings and why?
  3. In matrix factorization, what does k represent and why choose k << min(#users,#items)?

Answers: 1) Item-based CF predicts a user's rating for an item by aggregating that user's ratings on items similar to the target item, where item similarity is computed from other users' interaction patterns. 2) Pearson correlation uses mean-centred ratings to remove user bias so correlation reflects co-variation, not absolute level. 3) k is the number of latent factors (dimensions) capturing underlying features; choose k small to reduce overfitting and for dimensionality reduction while keeping expressive power.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?