Sub topic 16
Combining collaborative and content signals
This chapter assumes you know the user-item matrix, cosine similarity, and matrix factorization from the earlier chapters. It also assumes basic NumPy.
Why combine signals
Collaborative filtering uses only behavior: who rated or clicked what. It has nothing to go on for a new item with no ratings, or a new user with no history. Content-based methods use item attributes such as genre, tags, text, or price. They work for a brand-new item, but they cannot find patterns that attributes miss, such as two unrelated films that the same people enjoy.
A hybrid recommender uses both. It uses content to cover the cold start gaps and behavior to capture taste that attributes cannot describe.
Common hybrid designs
| Design | How it works | Typical use |
|---|---|---|
| Weighted | Blend the scores from each model with a weight | Simple, easy to tune |
| Switching | Pick one model by a rule, for example "use content if the item has fewer than 5 ratings" | Cold start handling |
| Feature augmentation | Feed content features into the collaborative model | Factorization with side information |
| Cascade | One model proposes candidates, the other re-ranks them | Large catalogs |
A weighted hybrid in code
The content model builds a user profile as the rating-weighted average of the features of items the user rated. It then scores every item by cosine similarity to that profile. The blend trusts the collaborative score more as an item collects ratings. The weight is alpha = n / (n + c), where n is the item's rating count and c controls how fast trust grows.
import numpy as np
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)
# Item features: [action, romance, comedy]
F = np.array([
[1, 0, 0],
[1, 0, 1],
[0, 1, 1],
[0, 1, 0],
], dtype=float)
def content_scores(R, F, user):
rated = R[user] > 0
profile = (R[user, rated] @ F[rated]) / R[user, rated].sum()
norms = np.linalg.norm(F, axis=1) * np.linalg.norm(profile)
norms[norms == 0] = 1.0
return (F @ profile) / norms
def hybrid(cf_row, content_row, counts, c=5):
alpha = counts / (counts + c)
return alpha * cf_row + (1 - alpha) * (1 + 4 * content_row)
counts = (R > 0).sum(axis=0)
user = 0
cf_row = np.array([4.8, 3.1, 2.0, 1.2]) # stand-in for P @ Q.T from a trained model
print(np.round(hybrid(cf_row, content_scores(R, F, user), counts), 2))The expression 1 + 4 * content_row maps a cosine value in [0, 1] onto the 1 to 5 rating scale, so both scores are comparable. Item 2 has only one rating, so its final score leans mostly on content.
Common errors
- Mismatched scales. Blending a 1 to 5 prediction with a 0 to 1 cosine value makes one signal dominate. Rescale first.
- One global weight. A fixed
alphaignores that items differ in how much data they have. Make the weight depend on interaction counts. - Tuning on the wrong split. Choose
alphaandcon a validation split, and report results on a separate test split. Evaluate cold items separately, because overall metrics hide cold start performance. - Weak features. Noisy or sparse attributes pull good collaborative scores down. Check that content-only ranking beats a popularity baseline before blending.
Exercise
In the code above, item 2 has 1 rating and item 0 has 4. With c=5, which item's score depends more on content, and what is each item's alpha?
Show answer
Item 2 depends more on content. Its alpha is 1/(1+5) ≈ 0.17, so about 83% of its score comes from content. Item 0 has alpha 4/(4+5) ≈ 0.44, so about 56% of its score comes from content.