Collaborative Filtering
Collaborative filtering recommends from collective behavior rather than item content alone. It assumes that users with similar histories, or items consumed by similar users, carry useful preference signal. The main families are user-based collaborative filtering, item-based collaborative filtering, and model-based methods such as matrix factorization.
Similarity-based prediction
For memory-based collaborative filtering, cosine similarity is common:
Here and are interaction vectors for two users or two items, depending on the method. The numerator counts aligned behavior, while the denominator normalizes for activity level so a heavy user is not similar to everyone merely because they interacted with many items.
A simple user-based score aggregates neighbor interactions:
The score estimates how strongly user may like item . Each neighbor contributes only if interacted with item , and the contribution is weighted by similarity to .
This operates on a utility matrix; factor models replace explicit neighbors with learned latent coordinates.
Worked example
The code below constructs four user interaction vectors, computes user 0’s cosine similarity to the others, and scores unseen items by similarity-weighted neighbor interactions. It is useful here because the recommended item comes from neighbor geometry, not from item content.
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
X = np.array([[1,1,0,0,0], [1,1,1,0,0], [0,0,1,1,1], [0,1,0,1,0]])
sim = cosine_similarity(X)[0]
scores = sim @ X
scores[X[0] > 0] = -1
print("user0_similarities", np.round(sim, 3).tolist())
print("recommend_item", int(np.argmax(scores)), "score", round(float(scores.max()), 3))Observed output:
user0_similarities [1.0, 0.816, 0.0, 0.5]
recommend_item 2 score 0.816User 1 is the closest neighbor and contributes the top unseen item. Hybrid recommenders add content features when behavior is too sparse.
Caveats
Collaborative filtering fails for new users and items without interactions, so cold-start strategies are mandatory. Similarity can reflect exposure and popularity, not preference. Neighborhood methods also become expensive without approximate retrieval or candidate pruning.
References
- Sarwar et al., 2001, Item-based Collaborative Filtering Recommendation Algorithms
- scikit-learn documentation: cosine_similarity
Nav