Item-Item Collaborative Filtering

medium
Unit 13 · Recommenders & Metrics0/3 to milestone

Amazon's classic recommender: "people who liked this also liked…". Compare items by the users who rated them, then predict a user's rating for an item from their ratings of similar items.

R is a users × items matrix where 0 means "not rated". Implement:

  • item_similarity(R): cosine similarity between item columns (treating missing as 0), with a zero diagonal. Items with no ratings have similarity 0 to everything.
  • predict(R, S, u, i, k=5): among the items user u has rated (excluding i), take the k with the highest similarity to i (ties: lower index), and return their similarity-weighted average rating, ∑jsijruj∑j∣sij∣\frac{\sum_j s_{ij} r_{uj}}{\sum_j |s_{ij}|}. Return 0.0 if the weights are all zero.
  • recommend(R, S, u, n, k=5): the n unrated items with the highest predicted ratings (ties: lower index)
#recommenders#similarity#collaborative-filtering2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Item-Item Collaborative Filtering · TechGradify