Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 38 ยท Collaborative Filtering

Item-based Collaborative Filtering

Implement item-based collaborative filtering: item-item similarity with adjusted cosine, rating prediction from a user's own ratings, top-N lists and 'because you liked' explanations.

Item-based collaborative filtering flips the viewpoint. Instead of asking "who is like this user?", it asks "which items are like the items this user already rated?" and then predicts the rating from the user's own ratings of those similar items. Amazon popularized it, mainly because it scales and stays stable better than user-based CF.

Why items instead of users?

  • Stability: item relationships change slowly ("Dune" stays similar to "Foundation"), while a user's neighborhood shifts with every click. You can precompute item similarities offline and refresh them daily.
  • Size: most platforms have far fewer items than users, so the item-item matrix is smaller.
  • Reliability: an item is rated by many users, so item-item similarities rest on more data than user-user ones.
  • Explanations: "because you rated Dune 5 stars" is concrete.

The algorithm

  1. For every item pair, compute similarity over users who rated both. Adjusted cosine (subtract each user's mean, then cosine) is the standard choice.
  2. To predict r(u, i), take the items j the user rated that are most similar to i (top k, positive similarity).
  3. Predict ฮฃ sim(i, j) ยท r(u, j) / ฮฃ |sim(i, j)|.
flowchart LR
    A["Item i to predict"] --> B["Similar items j"]
    B --> C["Keep the ones user u rated"]
    C --> D["Weighted average of r(u,j)"]
    D --> E["Predicted rating"]

Unlike user-based CF, the ratings being averaged come from the target user, so no mean-centering of neighbors is needed (you can still add a baseline, see the bias lesson).

Building the item-item matrix

Python Adjusted cosine similarity between items

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Predicting and evaluating

Python Item-based predictions vs baselines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With only about twelve ratings per user and a small k, the user often has few rated items similar to the target, so predictions are noisy: here the RMSE is about 0.87, barely better than the 0.88 of always guessing the global mean. The fixes are the topic of the next two lessons: a bias baseline and shrinkage.

Explaining with "because you liked"

Because the prediction is a weighted sum over the user's own rated items, the largest terms are the explanation.

Python Explain one recommendation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

User-based or item-based?

User-based Item-based
Similarity matrix users x users items x items
Stability neighbors change often item similarities stable
Best when few users, many items many users, fewer items
Serendipity higher lower (similar to what you rated)
Precompute offline hard easy

Item-based CF

Stable Item links change slowly
Precomputable Build offline, serve fast
Explainable Because you rated X

Adjusted cosine is the default similarity.

1 / 2

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In item-based CF, whose ratings are averaged to predict r(u, i)?

    The prediction is a similarity-weighted average of the target user's own ratings on the most similar items.
  2. 2. Why is item-item similarity usually more stable than user-user?

    That stability lets you precompute and cache item neighbors.
  3. 3. What does adjusted cosine subtract?

    Subtracting each user's average removes generous or harsh rating habits before comparing item columns.
  4. 4. A new item has no ratings. What is its item-based CF situation?

    Similarities need co-ratings, which a brand-new item does not have.

Summary

  • Item-based CF predicts a rating from the user's own ratings of items similar to the target.
  • Adjusted cosine over co-raters is the standard similarity; the matrix can be computed offline.
  • The top terms of the weighted sum explain the recommendation.
  • It scales and stays stable better than user-based CF, but new items still need another method.

Next: Bias Baselines and Rating Prediction