Sub topic 1
Recommendations with Collaborative Filtering
Prerequisites
Assume you know basic Python, NumPy arrays, vectors, dot products, matrices, averages, and basic statistics.
How collaborative filtering works
Collaborative filtering recommends items from patterns in many users' behavior. It does not need a detailed description of each item. Instead, it uses the idea that users with similar past behavior may prefer similar future items.
For example, if users who liked Item A also often liked Item B, the system can recommend Item B to someone who liked Item A. This differs from content-based recommendation, which recommends items because their attributes resemble items the same user already liked.
The main pipeline is:
feedback → user-item matrix → similarity or latent factors → predictions → recommendationsExplicit and implicit feedback
Explicit feedback is a direct opinion, such as a five-star rating, a like, or a dislike. Its value usually describes preference strength.
Implicit feedback is an observed action, such as a view, click, purchase, or time spent. It shows interest, but silence does not necessarily mean dislike. A user may not have seen an item.
This difference affects training. Explicit ratings can be treated as target values. Implicit events are often represented as positive interactions, with confidence based on event count or type.
The user-item matrix and why it is sparse
A user-item matrix has one row per user and one column per item. Each cell stores a rating or interaction value. Most users interact with only a small fraction of a catalog, so most cells are missing. This is called sparsity.
In a simple NumPy representation, zero can mean “not observed”:
import numpy as np
R = np.array([[5, 3, 0, 1],
[4, 0, 0, 1],
[1, 1, 0, 5]], dtype=float)
sparsity = np.mean(R == 0)
print(f"sparsity = {sparsity:.2f}")Algorithms must avoid interpreting those zeros as genuine preferences.
Measuring similarity
Cosine similarity compares the angle between two vectors. It is useful when the direction of interaction patterns matters more than their size.
Pearson correlation first centers each user's values around that user's mean, then measures whether their deviations move together. It can handle users who use rating scales differently.
Jaccard similarity compares binary interaction sets: the size of the intersection divided by the size of the union. It is useful for implicit data, such as sets of purchased items.
The choice depends on the data. Use Pearson for comparable co-rated values, cosine for vector patterns, and Jaccard for binary events.
Exercise
Why is a missing cell in an implicit-feedback matrix usually not a negative example?
Show answer
The user may not have encountered the item, so the absence of an event does not establish dislike.