Collaborative Filtering: User-Based and Item-Based
User similarity, item similarity, neighborhood methods, cosine similarity for ratings.
Collaborative Filtering: User-Based and Item-Based is a free Learn AI with Python lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Collaborative Filtering?
Collaborative filtering (CF) recommends items by learning from the behavior of many users. The core idea: people who agreed in the past tend to agree in the future. It needs no item descriptions, only the interaction history.
The User-Item Matrix
CF starts from a user-item matrix where rows are users, columns are items, and each cell holds a rating (or blank if unrated). This matrix is usually very sparse since each user rates few items.
import pandas as pd
matrix = ratings.pivot_table(
index="user_id", columns="item_id", values="rating"
)Two Flavors of CF
- User-based: find users similar to you, recommend what they liked.
- Item-based: find items similar to ones you liked, recommend those.
Both rely on a similarity measure between rows (users) or columns (items).
Cosine Similarity
Cosine similarity measures the angle between two rating vectors, ignoring magnitude. Values near 1 mean very similar taste, near 0 means unrelated.
from sklearn.metrics.pairwise import cosine_similarity
filled = matrix.fillna(0)
user_sim = cosine_similarity(filled) # user x userUser-Based Prediction
To predict your rating for an item, take a weighted average of how similar users rated it, weighting by similarity. More similar neighbors count more.
Prediction Formula
Predicted rating = sum(similarity * neighbor_rating) / sum(similarity), over neighbors who rated the item. Subtracting each user mean first (mean-centering) corrects for users who rate generally high or low.
import numpy as np
def predict(sims, ratings_for_item):
mask = ~np.isnan(ratings_for_item)
if sims[mask].sum() == 0:
return np.nan
return np.dot(sims[mask], ratings_for_item[mask]) / sims[mask].sum()Choosing Neighbors (k)
Instead of all users, keep only the top-k most similar neighbors. This reduces noise from dissimilar users and speeds up prediction. k is a tunable hyperparameter.
Item-Based Similarity
Item-based CF computes similarity between item columns instead of user rows. Recommend items most similar to those a user already liked.
item_sim = cosine_similarity(filled.T) # item x itemWhy Item-Based Is More Stable
Item-item relationships change slowly: the similarity between two movies stays roughly constant. User tastes and the user base shift faster. So item-based similarities can be precomputed and reused, giving more stable, scalable recommendations, which is why Amazon famously favored it.
The Cold-Start Problem
CF struggles with new users or items that have no ratings yet; there is nothing to compare. Common fixes include asking new users for a few ratings or falling back to content-based methods (covered later).
Generating Recommendations
Score every unrated item for a user, sort descending, and return the top N as recommendations.
scores = {item: predict_for(user, item)
for item in unrated_items(user)}
top_n = sorted(scores, key=scores.get, reverse=True)[:10]Quick Check
Test your collaborative filtering knowledge.
Recap
You built CF on a user-item matrix, computed cosine similarity, predicted ratings as a similarity-weighted average of neighbors, and saw why item-based CF is more stable than user-based. Next: matrix factorization with SVD.
Frequently asked questions
Is the “Collaborative Filtering: User-Based and Item-Based” lesson free?
Yes — the full text of “Collaborative Filtering: User-Based and Item-Based” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Collaborative Filtering: User-Based and Item-Based”?
User similarity, item similarity, neighborhood methods, cosine similarity for ratings. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Collaborative Filtering: User-Based and Item-Based” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Collaborative Filtering: User-Based and Item-Based
- Matrix Factorization with SVD
- Content-Based Filtering
- Hybrid Systems and Evaluation Metrics