Skip to main content

User and Item Neighborhoods

Neighborhood recommenders transfer evidence through similar users or items. Their appeal is local explanation: a score can be traced to particular neighbors. Their weakness is that sparse or biased interaction data makes “similarity” fragile.

Prerequisites​

Understand content-based recommendation, binary sets, and the distinction between observed positive actions and missing events.

Learning goals​

  • Build user-user Jaccard and item-item cosine neighborhoods from training positives.
  • Name the state stored for inference.
  • Apply minimum-overlap, shrinkage, and candidate rules deliberately.
  • Recognize sparse, popularity-dominated, and cold-start neighborhoods.

Model contract​

PartThis lesson's contract
ObjectiveNominate an unseen lesson supported by nearby training users or items.
Input representationBinary user-item positive-action sets from the shared fixture; impressions alone remain exposure evidence.
Learned statePositive sets, pairwise similarities, overlap counts, and the exact shrinkage/tie policy.
Inference pathFind top neighbors for the query, accumulate their unseen-item evidence, then filter to eligible items.
Evaluation boundarySimilarities use only 16 training rows; test events define later relevance and never neighbors.
Principal failure modesSparse overlap, hubs/popularity, unstable ties, feedback loops, quadratic indexes, and no vector for a cold user or cold item.

Worked example: two neighborhood views​

Positive actions are click, save, complete, and rating. Repeated actions collapse to one binary pair, so confidence strength is not silently mixed with similarity.

import csv
import math
from collections import defaultdict
from pathlib import Path

path = Path("tests/fixtures/recommender-systems/interactions.csv")
with path.open(encoding="utf-8", newline="") as handle:
rows = list(csv.DictReader(handle))

positive = {"click", "save", "complete", "rating"}
user_items = defaultdict(set)
item_users = defaultdict(set)
for row in rows:
if row["split"] == "train" and row["event_type"] in positive:
user_items[row["user_id"]].add(row["item_id"])
item_users[row["item_id"]].add(row["user_id"])

def jaccard(left, right):
union = left | right
return len(left & right) / len(union) if union else 0.0

def binary_cosine(left, right):
denominator = math.sqrt(len(left) * len(right))
return len(left & right) / denominator if denominator else 0.0

neighbors = sorted(
((user, jaccard(user_items["u2"], items)) for user, items in user_items.items() if user != "u2"),
key=lambda pair: (-pair[1], pair[0]),
)
candidates = ["i1", "i4", "i5", "i6"]
scores = []
for candidate in candidates:
score = sum(binary_cosine(item_users[seen], item_users[candidate]) for seen in user_items["u2"])
scores.append((candidate, score))
scores.sort(key=lambda pair: (-pair[1], pair[0]))
print("u2-neighbors=[" + ", ".join(f"('{user}', {score:.3f})" for user, score in neighbors) + "]")
print("u2-item-scores=[" + ", ".join(f"('{item}', {score:.3f})" for item, score in scores) + "]")

Output:

u2-neighbors=[('u3', 0.333), ('u1', 0.000), ('u4', 0.000)]
u2-item-scores=[('i4', 0.500), ('i1', 0.000), ('i5', 0.000), ('i6', 0.000)]

u3 overlaps with u2 on i3; i4 overlaps with i3 through u3. A production version should store overlap support and usually shrink low-support similarities toward zero. This one-match score is arithmetic evidence, not a reliable performance claim.

Limitations and common mistakes​

  • Similarity on exposure-biased data can reproduce the old policy.
  • Treating every non-pair as negative invents labels; missing events are not dislikes.
  • Repeated events, bot activity, or household accounts can dominate without a declared aggregation rule.
  • Exact all-pairs neighborhoods can be too expensive; pruning trades coverage for latency.
  • No universal model ranking follows from this tiny fixture or any one offline metric.

Practice​

Require at least two co-positive users before accepting an item similarity. Which scores disappear, and what fallback would keep the surface usable?

Next steps​

Matrix factorization replaces explicit neighbor tables with learned low-dimensional user and item representations.

Primary sources​

Checked 2026-10-17: Sarwar et al., “Item-Based Collaborative Filtering Recommendation Algorithms”, and Google, collaborative-filtering basics.