Chapter 1 · Part 1
The ratings matrix
Netflix's "because you watched…", Amazon's "customers also bought…", Spotify's Discover Weekly — they all run on one idea: people with similar taste like similar things. In this hands-on course you'll build that engine, called collaborative filtering, from scratch in NumPy. It's the code companion to the visual How Your Feed Knows You course, and it reuses the cosine similarity from the NumPy and semantic search courses.
The data: who rated what
A recommender's raw material is a ratings matrix: one row per user, one column per movie,
each cell a star rating. The catch — and the whole reason recommenders exist — is that it's
mostly empty. Nobody watches everything. We'll use 0 to mean "hasn't rated this."
Make sure NumPy is installed (pip install numpy), then create recommender.py:
import numpy as np
movies = ["The Matrix", "Inception", "Interstellar",
"Notting Hill", "Love Actually", "Pretty Woman"]
users = ["Alice", "Bob", "Carol", "Dave", "Eve", "Frank"]
# rows = users, columns = movies, 1-5 stars (0 = not yet rated)
R = np.array([
[5, 4, 0, 1, 0, 2], # Alice
[5, 5, 4, 2, 1, 0], # Bob
[4, 0, 5, 0, 2, 1], # Carol
[1, 0, 2, 5, 4, 0], # Dave
[0, 1, 0, 4, 5, 5], # Eve
[2, 1, 0, 5, 0, 4], # Frank
], dtype=float)
print(R.shape) # (6, 6) — 6 users, 6 moviesThere's already structure hiding in here
Look closely and you can see two camps. Alice, Bob and Carol rate the sci-fi films (The Matrix, Inception, Interstellar) highly and the rom-coms low. Dave, Eve and Frank do the opposite. A recommender's job is to discover that pattern automatically and use it to fill the blanks:
seen = R > 0
print("ratings so far:", int(seen.sum())) # 25
print("blanks to fill:", int((~seen).sum())) # 11Eleven empty cells — eleven chances to recommend. Alice hasn't rated Interstellar or Love Actually; which should we suggest? Intuitively Interstellar, because the people who share her taste loved it. The next chapters make "share her taste" precise. First: measuring similarity.