esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

Machine Learning Foundations / Advanced / 03.49

Recommendation systems: what comes next?

How shops, streaming sites and news feeds guess what you will like next: similar things, similar people, and hidden tastes learned from a table of ratings.

13 min readFree

Suggesting what people might like

The Embeddings lesson ended with a promise: recommendation systems, suggesting what people might like. This is that lesson. You already use these systems every day. The next video on your screen, the "people also bought" row in a shop and the order of a news feed are all picks made by one.

Wikipedia describes a recommender system as a type of information filtering system that aims to suggest items most relevant for some input or to a particular user. Its examples: playlists for streaming services, product picks for online shops, and items on social media.

Think of a good waiter who remembers what you ordered and what guests like you ordered, then says: "Try the lentil soup." A recommender does that for millions of people at once.

Key words

Item
Anything that can be suggested: a film, a song, a shoe, an article.
Rating
A number a user gives an item, such as 1 to 5 stars.
Content-based filtering
Suggest items that look like the ones you liked before.
Collaborative filtering
Suggest items that people with similar taste liked. "Collaborative" means many users help each other.
Similarity
A number that says how alike two things are. Higher means more alike.
Matrix
A table of numbers with rows and columns.
Cold start
The system knows too little about a new user or a new item to suggest anything good.
Implicit feedback
What you did, not what you said: a click, a purchase, a song played to the end.
Explicit feedback
What you said on purpose: a star rating or a thumbs up.

How big systems pick from billions

A shop may hold millions of items. You cannot score every one for every visitor in a blink. Google's course describes a common three-stage design.

  • 1. Candidate generation

    A quick first pass cuts a huge pile to a short list. Google's example: YouTube reduces billions of videos down to hundreds or thousands.

  • 2. Scoring

    A more careful model scores that short list and picks the set to show, on the order of 10 items.

  • 3. Re-ranking

    A last pass applies extra rules, such as removing items the user disliked and boosting fresher ones. Google says it can also help ensure diversity, freshness and fairness.

Content-based: more of the same

The simplest idea is to look at the items themselves. If you liked three space films, suggest another space film. Google's course puts it this way: content-based filtering uses item features to recommend other items similar to what the user likes, based on their previous actions or explicit feedback.

Their example is an app store. Each app has features, such as its category. The user has features too, from what they chose or installed. The system scores each app by the features it shares with the user, using no information about other users.

That has a good side and a bad side. It works even if you are the only user. But it can only suggest more of what you already like. It will never say: "people like you also love jazz."

Collaborative: people like you liked this

Collaborative filtering ignores what the items are. It only looks at who liked what. Wikipedia says it is based on the assumption that people who agreed in the past will agree in the future, and that they will like similar kinds of items as they liked in the past.

The data lives in one table. Rows are users, columns are items, and each cell holds a rating. Most cells are empty, because nobody watches everything. Those empty cells are what we want to fill in. Wikipedia notes that this user-item matrix can be extremely large and sparse, which makes the job harder.

  • User-based

    Find users whose past ratings look like yours. Use their ratings for the item to guess yours.

  • Item-based

    Find items that were rated alike by many people. If you liked item A, suggest the items most like A. Wikipedia's short form: users who bought x also bought y.

  • Matrix factorisation

    Learn a few hidden tastes for every user and item, and guess a cell from them.

Both the first two methods need a way to measure similarity. Wikipedia names two common ones: Pearson correlation and vector cosine similarity. We will use cosine.

A tiny table to work with

We invent five users and six films. Ratings run from 1 to 5. The films come in two moods: Rocket, Dawn and Chase are action; Letters, Spring and Paris are romance. The code prints a dot for a cell nobody has filled.

Python
users = ["Ana", "Ben", "Cara", "Dev", "Eli"]
items = ["Rocket", "Dawn", "Chase", "Letters", "Spring", "Paris"]
# 0 means "not rated yet"
R = [
    [5, 4, 0, 1, 2, 1],   # Ana
    [4, 5, 5, 2, 1, 0],   # Ben
    [0, 1, 2, 5, 4, 5],   # Cara
    [1, 2, 1, 4, 0, 4],   # Dev
    [5, 0, 4, 1, 1, 2],   # Eli
]

for name, row in zip(users, R):
    print(f"{name:5}", " ".join("." if r == 0 else str(r) for r in row))
print("missing cells:", sum(r == 0 for row in R for r in row), "of", 5 * 6)

Output

Ana   5 4 . 1 2 1
Ben   4 5 5 2 1 .
Cara  . 1 2 5 4 5
Dev   1 2 1 4 . 4
Eli   5 . 4 1 1 2
missing cells: 5 of 30

Five of the 30 cells are empty. Look at Ana: she loves Rocket and Dawn, and gave romance low marks. Her cell for Chase is empty. Your eyes already guess a high number. Let the computer do the same.

Item-based: find the twin films

Two films are similar if the same people rated them in a similar way. Treat each film as the list of ratings it got. Cosine similarity checks whether two lists point the same way: 1 for the same direction, lower as they drift apart.

sim(a,b)=a⋅b∥a∥ ∥b∥\text{sim}(a, b) = \frac{a \cdot b}{\lVert a \rVert \, \lVert b \rVert}

In words: multiply the two lists number by number and add it up, then divide by the length of each list. We use only the users who rated both films.

  1. 1

    1. Score every other film against Chase

    Cosine similarity, using users who rated both.

  2. 2

    2. Keep Ana's two closest films

    Of the films Ana has rated, keep the two most like Chase.

  3. 3

    3. Take a weighted average

    Average Ana's ratings of those two, with more weight on the more similar film.

Python
import math

users = ["Ana", "Ben", "Cara", "Dev", "Eli"]
items = ["Rocket", "Dawn", "Chase", "Letters", "Spring", "Paris"]
R = [
    [5, 4, 0, 1, 2, 1],
    [4, 5, 5, 2, 1, 0],
    [0, 1, 2, 5, 4, 5],
    [1, 2, 1, 4, 0, 4],
    [5, 0, 4, 1, 1, 2],
]

def cosine(i, j):
    # compare two items using only the users who rated both
    pairs = [(row[i], row[j]) for row in R if row[i] and row[j]]
    dot = sum(a * b for a, b in pairs)
    na = math.sqrt(sum(a * a for a, _ in pairs))
    nb = math.sqrt(sum(b * b for _, b in pairs))
    return dot / (na * nb)

def predict(u, i, k=2):
    # the user's own ratings of the k most similar items, weighted by similarity
    near = sorted(((cosine(i, j), R[u][j]) for j in range(6) if R[u][j] and j != i), reverse=True)[:k]
    return sum(s * r for s, r in near) / sum(s for s, _ in near)

print("similarity to Chase:")
for j, name in enumerate(items):
    if j != 2:
        print(f"  {name:8}{cosine(2, j):.2f}")
print("Ana on Chase:", round(predict(0, 2), 2))

Output

similarity to Chase:
  Rocket  0.98
  Dawn    0.97
  Letters 0.61
  Spring  0.60
  Paris   0.72
Ana on Chase: 4.5

Chase looks most like Rocket (0.98) and Dawn (0.97), the other action films. The romance films score lower. Ana rated Rocket 5 and Dawn 4, so her predicted rating for Chase is 4.5. A fan of action films gets an action film. It works.

Tip

Why only the two closest films? Ratings are all positive, so every pair of films looks somewhat alike, even action against romance (0.60 to 0.72 here). If we averaged over every film, the romance films would drag Ana's guess down. Keeping the closest few is the standard fix.

Matrix factorisation: hidden tastes

Why does Ana like action films? A hidden taste: she likes action. Each film has a hidden amount of action. A high taste meeting a high amount gives a high rating.

Matrix factorisation turns that picture into numbers. Wikipedia says it works by decomposing the user-item interaction matrix into the product of two lower dimensionality rectangular matrices. Google's course calls it a simple embedding model: one embedding (a short list of numbers) for every user, one for every item, learned so that their dot product is close to the real rating. The Embeddings lesson showed how a short list of numbers can stand for a thing.

r^ui=pu⋅qi=∑kpuk qik\hat{r}_{ui} = p_u \cdot q_i = \sum_{k} p_{uk}\, q_{ik}

In words: the guess for user u and item i is the user's list of tastes multiplied with the item's list of traits, number by number, then added. Wikipedia says the approach became widely known during the Netflix Prize, after Simon Funk shared it in a 2006 blog post.

How do we find the numbers? We start with small random ones. Then, for each rating we know, we compare the guess with the truth and nudge the numbers a little to shrink the gap. This is gradient descent: many small steps downhill on the error. Empty cells are skipped while learning, and are filled in afterwards. We use 2 hidden factors, a fixed random seed and 2,000 rounds.

Python
import random

users = ["Ana", "Ben", "Cara", "Dev", "Eli"]
items = ["Rocket", "Dawn", "Chase", "Letters", "Spring", "Paris"]
R = [
    [5, 4, 0, 1, 2, 1],
    [4, 5, 5, 2, 1, 0],
    [0, 1, 2, 5, 4, 5],
    [1, 2, 1, 4, 0, 4],
    [5, 0, 4, 1, 1, 2],
]
known = [(u, i) for u in range(5) for i in range(6) if R[u][i]]

random.seed(0)
K = 2
P = [[random.uniform(0.1, 0.9) for _ in range(K)] for _ in range(5)]   # one row per user
Q = [[random.uniform(0.1, 0.9) for _ in range(K)] for _ in range(6)]   # one row per item

def score(u, i):
    return sum(P[u][k] * Q[i][k] for k in range(K))

lr, reg = 0.02, 0.02
for epoch in range(2001):
    for u, i in known:
        err = R[u][i] - score(u, i)
        for k in range(K):
            p, q = P[u][k], Q[i][k]
            P[u][k] += lr * (err * q - reg * p)
            Q[i][k] += lr * (err * p - reg * q)
    if epoch % 500 == 0:
        rmse = (sum((R[u][i] - score(u, i)) ** 2 for u, i in known) / len(known)) ** 0.5
        print(f"epoch {epoch:4}  error on known ratings {rmse:.3f}")

print("hidden tastes (2 numbers each):")
for u, name in enumerate(users):
    print(f"  {name:5}", [round(x, 2) for x in P[u]])
print("predictions for the empty cells:")
for u in range(5):
    for i in range(6):
        if not R[u][i]:
            print(f"  {users[u]:5}{items[i]:8}{score(u, i):.2f}")

Output

epoch    0  error on known ratings 2.550
epoch  500  error on known ratings 0.410
epoch 1000  error on known ratings 0.410
epoch 1500  error on known ratings 0.410
epoch 2000  error on known ratings 0.410
hidden tastes (2 numbers each):
  Ana   [1.91, 0.48]
  Ben   [1.96, 0.73]
  Cara  [-0.03, 2.43]
  Dev   [0.13, 1.9]
  Eli   [1.94, 0.53]
predictions for the empty cells:
  Ana  Chase   4.22
  Ben  Paris   2.00
  Cara Rocket  0.73
  Dev  Spring  3.07
  Eli  Dawn    4.48

The error on the known ratings falls from 2.550 to 0.410 and then stops improving. Look at the hidden tastes. Ana, Ben and Eli have a big first number (about 1.9). Cara and Dev have a big second number (1.9 to 2.4). Nobody told the program about action and romance. It found the two groups from the ratings alone.

The empty cells now have guesses. Ana on Chase is 4.22, close to the 4.5 from the item-based method. Eli on Dawn is 4.48. Ben on Paris is 2.00, which fits a person who rates romance low. Dev on Spring is 3.07, a bit low, since Dev gave romance films 4s. With only 25 known ratings, expect rough guesses.

Tip

Cara on Rocket came out as 0.73, below the 1 to 5 range. Real systems clip guesses to the rating scale, or only use the order of the scores to rank films.

Cold start, and what people really tell you

Every method above needs data. A new user has no ratings. A new film has no raters. This is the cold start problem. Wikipedia's comparison of two music services shows it: Last.fm needs a large amount of information about a user to make accurate recommendations, which it calls an example of the cold start problem, while Pandora needs very little information to start but is far more limited in scope. Wikipedia adds that for a new user the learned factors are not available, so no recommendations can be computed.

Feedback comes in two kinds. Explicit feedback is a star rating. Implicit feedback is behaviour: what a user bought, what music they listened to. Wikipedia says collaborative filtering can be based on such implicit observations of normal user behaviour, as opposed to the artificial behaviour imposed by a rating task. Wikipedia notes that ratings are not available in every domain: users of citation recommenders typically do not rate a citation. The catch with behaviour is that it is less clear, since a click may not mean a like.

Is it any good? Precision at k

A recommender shows a short list, so we judge the top of the list. The Precision lesson gave you precision: of the things you said yes to, how many were right. Precision at k asks it of the first k items shown. Wikipedia: P@10 corresponds to the number of relevant results among the top 10 retrieved documents. It also lists the weak spots: it ignores where the good items sit inside the top k, and a perfect system scores less than 1 if fewer than k items are relevant.

Wikipedia adds that precision and recall are useful to assess a recommendation method, and that offline test results often do not correlate with actual user satisfaction.

Python
ranked = ["Chase", "Paris", "Dawn", "Spring", "Rocket"]   # what we showed, best guess first
liked = {"Chase", "Dawn", "Rocket"}                        # what the user really liked

def precision_at_k(ranked, liked, k):
    top = ranked[:k]
    return sum(item in liked for item in top) / k

for k in (1, 3, 5):
    print(f"precision@{k} = {precision_at_k(ranked, liked, k):.2f}")

Output

precision@1 = 1.00
precision@3 = 0.67
precision@5 = 0.60

We showed five films. Three were truly liked. The top film was a hit (1.00). In the top three, two of three were hits (0.67). In all five, three of five (0.60).

Filter bubbles and fairness

A system that only shows what you already like can shrink your world. Wikipedia defines a filter bubble as a state of intellectual isolation that arises when personalized searches, recommendation systems, and algorithmic curation selectively present information to each user. Google's course lists fairness, diversity and freshness as goals for the re-ranking stage. Accuracy alone is not the whole job.

Quick check

Cara has not rated Rocket. Which method would suggest it to her because users like her liked it?

Next: explainable AI, seeing why a model decided what it did.

Sources

How is this lesson?

Last updated on 16 November 2026

Learn with me

Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.