Prepare a user-item matrix
Collect explicit ratings or implicit signals and pivot them into a dense user-by-item matrix, filling missing entries appropriately.
Recommend products by nearest users. Rank unseen items by neighbour ratings.
User-based collaborative filtering recommends products by finding users with similar rating patterns and suggesting items those neighbours liked. Important failure modes include sparse or biased ratings, cold-start items and users, popularity biases and the need for offline ranking metrics.
Learning concept · Not a validated service
User-based collaborative filtering recommends products by finding users with similar rating patterns and suggesting items those neighbours liked. Important failure modes include sparse or biased ratings, cold-start items and users, popularity biases and the need for offline ranking metrics.
A proposed extension for a small retail catalogue: surface personalised product suggestions for signed-in shoppers, with fallbacks for new users. This extension is not delivered by the supplied starter code.
Collect explicit ratings or implicit signals and pivot them into a dense user-by-item matrix, filling missing entries appropriately.
Measure similarity between user rows (cosine or Pearson) to find nearest neighbours for each target user.
Aggregate neighbour ratings or weighted averages to score items the target user has not seen, then sort by score.
Use offline ranking metrics (Precision@K, Recall@K, NDCG) and inspect cold-start and popularity-driven failures before deployment.
The starter builds a small DataFrame of hand-crafted user-item ratings, pivots it to a user-by-item matrix, computes cosine similarity between users, selects the single most similar neighbour for a target user, and recommends that neighbour's unseen items (printing the top three).
matrix = ratings.pivot_table(index="user", columns="item", values="rating").fillna(0)
user_similarity = pd.DataFrame(
cosine_similarity(matrix),
index=matrix.index,
columns=matrix.index,
)
target_user = "user_1"
neighbor = user_similarity[target_user].drop(target_user).idxmax()
unseen_items = matrix.loc[target_user][matrix.loc[target_user] == 0].index
scores = matrix.loc[neighbor, unseen_items].sort_values(ascending=False)The starter uses a tiny synthetic table of six users/items and does not include train/test splits, offline ranking metrics, item metadata, business rules (diversity or safety), or any deployment/serving logic; it cannot validate real-world recommendation performance or cold-start behaviour.
A proposed extension: replace the toy ratings with retail transaction histories and item metadata, compute k-nearest neighbours, rank top-K items per user and expose suggestions via a small API or homepage card that logs clicks for feedback.
This is a potential application pathway, not a delivered product. The supplied starter does not implement this service or validate it with real users.
Networkers Home runs this topic inside its AI Engineer Course, with placement support and hands-on labs.