Sub topic 1
How collaborative filtering works
Prerequisites
This chapter assumes you know basic Python, can read NumPy arrays, and understand vectors, dot products, and averages. No recommender-system background is needed.
The core idea
Collaborative filtering (CF) predicts what a user will like from the recorded behavior of many users. It does not need to know what an item is. It only needs to know who interacted with it and how.
The assumption behind it is simple. If two users agreed in the past, they are likely to agree again. If Ana and Ben both loved three films, and Ben also loved a fourth, Ana will probably like it too.
CF is one of two broad families of recommenders:
| Approach | Input it uses | Example signal | Main weakness |
|---|---|---|---|
| Content-based | Item attributes and a user's own history | "You liked sci-fi, here is more sci-fi" | Needs good item features and tends to repeat what the user already knows |
| Collaborative filtering | Interactions of many users with items | "People like you liked this" | Needs enough interaction data |
A content-based system compares item descriptions. A collaborative system compares behavior. This is why CF can recommend a film from a genre the user has never touched, as long as similar users enjoyed it.
What the data looks like
CF works on a table of interactions. Each row is a user, each column is an item, and each cell holds a rating or a record of an action. Most cells are empty, because nobody sees every item. Later chapters build this matrix in detail. For now, here is a tiny one in code:
import numpy as np
# Rows are users, columns are items. 0 means "not rated".
R = np.array([
[5, 3, 0, 1],
[4, 0, 0, 1],
[1, 1, 0, 5],
[1, 0, 0, 4],
[0, 1, 5, 4],
], dtype=float)
observed = R > 0
sparsity = 1.0 - observed.sum() / R.size
print(f"{observed.sum()} ratings, sparsity = {sparsity:.2f}")This prints 14 ratings, sparsity = 0.30. The task of CF is to fill in the zeros: guess which unrated cells a user would rate highly.
Two ways to fill the gaps
Most CF methods fall into two groups.
- Neighborhood methods find users (or items) that are similar and average their ratings. They are easy to explain and need no training.
- Latent factor methods learn a short vector of numbers for every user and item. A high dot product means a good match. Matrix factorization is the best-known example.
The diagram shows the shared workflow.
Common errors at this stage
- Confusing missing with negative. An unrated item is unknown, not disliked.
- Expecting CF to work with no history. A brand-new user or item has no interactions to compare. This is the cold start problem, covered later.
- Judging only by accuracy. A model can predict ratings well and still produce dull lists. Later chapters introduce ranking metrics for this reason.
What comes next
The next chapters separate the kinds of feedback you can collect, then build similarity measures, neighborhood predictors, and factorization models on top of the matrix above.
Exercise
A streaming service has item descriptions (genre, cast, director) for every film. Why might it still choose collaborative filtering over a content-based approach for some recommendations?
Show answer
Collaborative filtering uses behavior, so it can surface films whose attributes the user has never shown interest in, because similar users enjoyed them. It also captures qualities that are hard to put in metadata, such as tone or style. Its cost is that it needs enough interaction data, and it fails for new users or items with no history.