Sub topic 6
Item-based collaborative filtering
This chapter assumes you know basic Python, NumPy arrays, dot products, and cosine similarity from the previous chapters. It also assumes you have the user-item matrix R, where 0 marks a missing rating.
The idea
Item-based collaborative filtering predicts a user's rating for a target item from the ratings that same user gave to similar items. The similarity between two items comes from how all users rated them. Two items are similar if the same users tend to rate them alike.
User-based filtering compares rows of R, and item-based filtering compares columns. Item-based filtering is often preferred for two reasons:
- Catalogs usually change more slowly than user behavior, so item similarities stay valid longer.
- Items often have many more ratings than a single user has, so each similarity rests on more evidence.
Step 1: item-item similarity
Cosine similarity between columns i and j is their dot product divided by the product of their norms. In matrix form, R.T @ R gives all the dot products at once. Dividing by the outer product of the column norms gives every cosine.
Step 2: predict a rating
For user u and target item i, take the items u has rated, excluding i. Keep the k with the highest similarity to i. The prediction is
prediction = sum(sim(i, j) × rating(u, j)) / sum(sim(i, j))
where j ranges over those k items.
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)
def item_cosine(R):
norms = np.linalg.norm(R, axis=0)
norms[norms == 0] = 1.0
return (R.T @ R) / np.outer(norms, norms)
def predict(R, S, user, item, k=2):
rated = np.where(R[user] > 0)[0]
rated = rated[rated != item]
if len(rated) == 0:
return float(R[R > 0].mean())
top = rated[np.argsort(S[item, rated])[::-1][:k]]
w = S[item, top]
if w.sum() == 0:
return float(R[R > 0].mean())
return float(w @ R[user, top] / w.sum())
S = item_cosine(R)
print(predict(R, S, user=0, item=2))The script prints about 1.73. User 0 rated items 0, 1, and 3. Item 2 has similarity 0 to item 0, 0.30 to item 1, and 0.52 to item 3. With k=2, the prediction averages the ratings for items 3 and 1 (1 and 3) with those weights. Item 3 gets the larger weight, so the result lands nearer 1 than 3.
Common errors
- Using the target item as a neighbor. The code removes it from
rated. Without that, the item's similarity to itself (1.0) would dominate. - Zero-weight fallbacks. If no rated item has positive similarity, the weights sum to 0 and the division fails. The code falls back to the global mean.
- Treating 0 as a rating. Plain cosine on this matrix counts missing entries as zeros, which is a rough approximation. Mean-centering or Pearson similarity computed only on co-rated users handles missing data more carefully.
- Tiny overlaps. Two items rated by one common user can look perfectly similar. Shrink similarities for small overlaps, or require a minimum number of shared raters.
Exercise
In the matrix above, why does item 0 contribute nothing to the prediction for user 0 on item 2, even though user 0 rated item 0 with a 5?
Show answer
Items 0 and 2 have no users in common. Item 2 was rated only by user 4, and user 4 did not rate item 0. Their dot product is 0, so their cosine similarity is 0. Item 0's weight is 0, and it also falls outside the top 2 neighbors.