Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 38 ยท Collaborative Filtering

Item-based CF for Implicit Feedback

Build an item-item recommender for clicks and purchases: binary interaction matrix, cosine and conditional-probability similarity, popularity damping and hit-rate evaluation.

Most real systems have no star ratings. They have clicks, views, purchases and plays: implicit feedback. There is no number to predict, and a missing interaction does not mean dislike. The goal changes from "predict the rating" to "rank the items the user is most likely to interact with next".

This lesson adapts item-based collaborative filtering to that setting using sparse matrices, evaluates it correctly with a leave-last-out split, and shows two similarity variants and a popularity fix. Background on the data type is in Explicit and Implicit Feedback.

The data

The user-item matrix X is binary: X[u, i] = 1 if user u interacted with item i. It is stored as a SciPy sparse CSR matrix because over 99% of cells are empty.

Item-item similarity becomes a statement about co-occurrence: two items are similar when the same users interact with both. With X binary, Xแต€ X counts exactly those co-occurrences.

Python Binary data, co-occurrence counts

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.

Two similarity choices

Cosine on binary vectors: cos(i, j) = co(i, j) / sqrt(n_i ยท n_j). Dividing by the geometric mean of the popularities stops a blockbuster from being similar to everything.

Conditional probability (as in Deshpande and Karypis): P(j | i) = co(i, j) / n_i, "of the users who took i, how many also took j?" It is asymmetric and tends to favor popular j. The common fix is to normalize by n_j ** ฮฑ with 0 < ฮฑ < 1:

sim(i, j) = co(i, j) / (n_i ยท n_j ** ฮฑ)

ฮฑ = 0 is plain conditional probability (popularity-biased), ฮฑ = 0.5 is cosine-like.

Scoring and leave-last-out evaluation

To recommend for user u, sum the similarity of every candidate to all items the user has: score(j) = ฮฃ_{i โˆˆ history(u)} sim(i, j). Exclude items already seen. To evaluate, hold out one interaction per user, recommend top 10 from the rest, and count how often the held-out item appears: HitRate@10.

Python Item-kNN with HitRate@10

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.

The item-kNN models beat the most-popular list because they capture the taste groups. Compare alpha values: with 0 the popular items dominate every score, with 0.5 to 0.8 the similarity is more personal. The right value depends on your data, so choose it on validation interactions.

Practical improvements

  • Normalize users: a user with 500 interactions would dominate co-occurrence counts. Weight each user's row by 1 / sqrt(len(history)) so heavy users count less.
  • Confidence weights: replace 1 with a value from the action: view = 1, add to cart = 3, purchase = 5. Dwell time and recency can scale it further. ALS later builds this confidence idea into the model itself.
  • Recent history: score from only the last N interactions to follow the user's current intent.
  • Remove what is already known: do not recommend things already bought, unless the item is repeatable (groceries, music).
Python Weighting heavy users down

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.

The single user who touched everything creates almost 38% of all co-occurrence mass in the plain version and about 9% after weighting.

Implicit item-kNN

Binary matrix 1 = interacted, sparse
Co-occurrence Xแต€ X counts shared users
Normalize Divide by popularity
Sum scores Over the user's history

Exclude items the user already has.

1 / 2

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. With a binary user-item matrix X, what does Xแต€ X contain?

    Entry (i, j) sums x_ui * x_uj over users, which counts co-occurrences.
  2. 2. Why divide co-occurrence by popularity terms?

    Without normalization, popular items co-occur with everything and dominate recommendations.
  3. 3. What does HitRate@10 measure in leave-last-out evaluation?

    It is the fraction of users for whom the next item was among the 10 recommended.
  4. 4. Why exclude seen items before ranking?

    Known items score highly because they co-occur with everything the user did, but showing them is wasted space.

Summary

  • Implicit feedback gives a sparse binary matrix and a ranking task, not rating prediction.
  • Item similarity comes from normalized co-occurrence; sum similarities over the user's history to score candidates.
  • Down-weight heavy users, use confidence weights and evaluate with a leave-last-out HitRate against a popularity baseline.

Next: Scaling Neighborhood Methods