Algorithms · Counting & data

Collaborative filtering

People like you also liked: the idea behind Netflix-style picks. You don’t need to understand movies to recommend them. You need to understand people who watch them.

The problem

Open a streaming app and you face a huge catalogue and a few seconds of patience. Nobody can browse it all. The service has to guess which few titles you'd enjoy, and it has to guess for each member separately.

Netflix cared enough about this to run the Netflix Prize (2006–2009): a $1M competition to improve its rating predictions by 10%. Predicting what you'd rate a title you haven't seen was the core puzzle.

The obvious way

Just recommend whatever is most popular: average everyone's ratings and show the top title. It's easy, and it works for nobody in particular. A sci-fi fan gets the romance everyone else loved, because the average has no idea who is asking.

The clever idea

Think of asking a friend for a movie tip. You don't poll the whole town. You ask the friend whose taste matches yours, the one who loved the same films you did. Now do that with math: find members whose past ratings look like yours, then borrow their opinions about what you haven't seen.

To measure "similar taste" we treat each person's ratings as an arrow and check whether two arrows point the same way. That score is cosine similarity: near 1 means alike, near 0 means unrelated.

How the code does it

Maya loves space and robots and dislikes romance. The cosine function gives Ben and Cleo scores near 1 for her, and Dev, Eli and Fay (romance and comedy fans) much lower ones. That's the "friend with your taste" step, done with numbers.

Then the prediction asks only Maya's closest twins who saw the title, and averages their ratings, weighting closer twins more. Compare it with the popular score, which just averages everyone. Run it and look at the two charts: popularity picks Cold Case (3.75), while Maya's twins give Star Run about 5.

Trade-offs

A newcomer has no ratings, so there are no twins to find (the "cold start" problem), and comparing every pair of members gets costly as they grow. The Netflix Prize shows the other side: winning approaches blended many models, notably matrix factorisation, yet Netflix has said it ultimately did not deploy the full winning ensemble because of engineering cost. Real systems use many signals, so treat this as one ingredient.

Where else you’ll see it

The same "people like you" trick shows up in music playlists, online shops' "customers also bought" shelves, and book suggestions. Anywhere many people rate or choose from many items, similar tastes can fill in the blanks.

Why does the clever method pick Star Run for Maya when "popular" picks Cold Case?

  • It listens most to people whose past ratings match Maya's
  • It picks the title with the most ratings overall
  • It picks whichever title has the lowest average
Show the answer

It listens most to people whose past ratings match Maya's. Ben and Cleo rate like Maya and both gave Star Run a 5. Popularity averages everyone, including people with opposite taste.

Change it and see
  • Change k=2 to k=4 in predict
  • Make Dev's ratings match Maya's
  • Set Maya's Love Letters rating to 5

The code

Python, with what each line means beside it. It compares the clever way with recommending the most popular titles.

import mathBring in the math toolbox
titles = ["Space Saga", "Star Run", "Robo Pals", "Love Letters",List the eight titles
"Chef Wars", "Laugh Riot", "Cold Case", "Dark Manor"]Finish the title list
names = ["Maya", "Ben", "Cleo", "Dev", "Eli", "Fay"]List the six people
N = NoneLet N mean no rating
R = [[5, N, 5, 1, N, 2, N, N],Maya's ratings, one per title
[5, 5, 4, 2, 1, N, 2, N],Ben's ratings
[4, 5, 5, N, 2, 1, N, 2],Cleo's ratings
[1, N, 2, 5, 5, 4, 4, N],Dev's ratings
[2, 1, N, 5, 4, 5, 5, 3],Eli's ratings
[N, 2, 1, 4, 5, 5, 4, 4]]Fay's ratings; table ends
show([[v or 0 for v in row] for row in R], "ratings (0 = unseen)")Draw the ratings as a grid
def cosine(a, b):Define similarity between two rating rows
both = [(x, y) for x, y in zip(a, b) if x and y]Keep only titles both people rated
if not both:If they share nothing,
return 0call them unrelated (zero)
dot = sum(x * y for x, y in both)Multiply matching ratings and add up
na = math.sqrt(sum(x * x for x, _ in both))Measure the first person's rating length
nb = math.sqrt(sum(y * y for _, y in both))Measure the second person's length
return dot / (na * nb)Divide: closer to 1 means more alike
sims = {u: cosine(R[0], R[u]) for u in range(1, 6)}Score everyone's similarity to Maya
show({names[u]: round(s, 2) for u, s in sims.items()}, "similarity to Maya")Show those scores in a table
def popular(t):Define the obvious popularity score
col = [row[t] for row in R[1:] if row[t]]Collect everyone else's ratings for the title
return sum(col) / len(col)Return their plain average
def predict(t, k=2):Define the clever prediction
fans = sorted((u for u in sims if R[u][t]), key=lambda u: -sims[u])[:k]Find the most similar people who rated it
return sum(sims[u] * R[u][t] for u in fans) / sum(sims[u] for u in fans)Average their ratings, trusting closer twins more
unseen = [t for t, v in enumerate(R[0]) if v is None]Find titles Maya hasn't rated
print("Maya hasn't seen:", [titles[t] for t in unseen])Print those titles
show([round(popular(t), 2) for t in unseen], "obvious: popular")Chart the popularity scores
show([round(predict(t), 2) for t in unseen], "clever: people like Maya")Chart the personalised predictions
print("Popular pick:", titles[max(unseen, key=popular)])Print the popular winner
print("Clever pick:", titles[max(unseen, key=predict)])Print the clever winner

Read more

Run it. Then change it.

The code runs right in the page and draws as it goes. Free, nothing to install.

More on counting & data