Collaborative Filtering
You get picks from people whose past ratings match yours, not from item descriptions. Overlap in behavior decides the next recommendation.
Why Does This Exist?
A store with thousands of titles cannot hand read every plot summary for every shopper. Genre matching is the obvious fix. You liked one space opera, so you get ten more space operas. It works until it turns stale. You'll never meet the cookbook your fellow sci-fi readers all bought.
Collaborative filtering skips the item entirely and reads the crowd. Call the ratings table , with users as rows and items as columns. Entry is the stars user gave item , from 1 to 5, with most cells empty because nobody rates more than a sliver of the catalog. Two rows with the same pattern of highs and lows probably want the same next thing, even when the items share no genre at all.
You need one prerequisite to follow the math. Cosine similarity measures the angle between two rows, ignoring how generous each rater is. The full trade-offs of that choice live in distance and similarity metrics.
This page covers neighbourhood counting with that score, user based and item based. Association rule mining counts which items co-occur before anyone personalizes. Matrix factorization is the direct sequel and fills the empty cells with learned taste profiles. Recommender system architecture shows where this step sits, as fast retrieval before careful ranking.
Think of It Like This
The office lunch table
You start a new job and know nobody. At lunch you watch two tables. Table Ben orders what you ordered all week, same noodles, same extra chili, same distaste for the bland sandwich. Table Cara orders the opposite, sandwiches daily, noodles never.
Friday brings a new special, garlic shrimp. You have no read on it. You join Table Ben. You are betting that past plate overlap predicts this plate too.
The mapping is direct. Diners are users, dishes are items, finished plates are high ratings, the new special is the unseen item, and picking Ben's table is picking the nearest neighbour.
Where it stops: Ben's peanut tolerance is not yours. Shared history says you'll like the flavor, not that the dish is safe for you. And when the kitchen invents a dish nobody has tried, both tables stare blankly. No history means no neighbour, which is the cold start you'll meet below.
How It Actually Works
Neighbourhood filtering predicts your missing stars from the rows or columns that already agree with you. User based asks who rates like you. Item based asks which items get rated by the same people. Both use the same score, just transposed.
The ratings matrix and the similarity score
Write for one user row, meaning the full list of that user's stars across all items. Write for the similarity between users and , a number where larger means closer taste. With cosine similarity, that number is the dot product scaled by both lengths:
Here multiplies matching entries and adds them, and is the length, the square root of the sum of squares. Cosine cares about direction, not generosity, so a harsh rater and a generous rater with the same pattern still match. That is also its blind spot. Direction ignores scale, so two users who share one blockbuster can look identical. K-nearest neighbors makes the same bet in classification, vote among the closest rows.
Prediction is then a weighted vote. For user based, take the closest users who rated the target item and average their stars weighted by . For item based, take the items you already rated, weight each by its item to item similarity with the target, and average. Item similarities change slowly, so shops precompute them overnight and serve them fast all day.
Worked example: Ana matches Ben at 0.77
Three users rate four films from 1 to 5, with 0 meaning not seen. The diagram draws this exact table:
- Ana: for Dune 1, Dune 2, Romance, Alien
- Ben:
- Cara:
Ana's length covers two ratings:
Ben's length covers four:
Their dot counts only the shared signal:
So:
Cara's length is . Her dot with Ana is , and , so:
Ben wins by a mile. Ben gave Alien 5 stars and Ana has not seen it, so Alien at 5 becomes Ana's recommendation. That single focal cell is the picture at the top of the page.
Item based flips the comparison
Read columns instead of rows. Dune 1 is down Ana, Ben, Cara. Dune 2 is . Romance is .
Same verdict from the other side. Space operas share an audience, romance does not. The trade-off is operational. User based compares every pair of users, which buckles when users number in the millions and tastes shift daily. Item based compares items, which are fewer and steadier, so teams precompute that matrix once and reuse it. When sparsity gets extreme, both degrade into popularity contests, and that is when you move to matrix factorization.
Code
The point is the check. Recompute the two similarities from the table above and confirm the hand numbers 0.77 and 0.36.
import math
ana: list[float] = [5.0, 4.0, 0.0, 0.0]ben: list[float] = [5.0, 4.0, 2.0, 5.0]cara: list[float] = [1.0, 2.0, 5.0, 1.0]
def cosine(a: list[float], b: list[float]) -> float: dot: float = sum(x * y for x, y in zip(a, b)) norm_a: float = math.sqrt(sum(x * x for x in a)) norm_b: float = math.sqrt(sum(y * y for y in b)) return dot / (norm_a * norm_b)
sim_ben: float = cosine(ana, ben)sim_cara: float = cosine(ana, cara)print(f"{sim_ben:.2f} {sim_cara:.2f}") # -> 0.77 0.36print(f"recommend Alien at {ben[3]:.0f} stars") # -> recommend Alien at 5 starsThe printout matches the hand computation. Ben at 0.77 beats Cara at 0.36, so Ana inherits Ben's 5 for Alien.
Watch Out For
A new row with no history gives no neighbours
A brand new user has a row of zeros, and a brand new film has a column of zeros. Every dot is 0, every similarity is 0 or undefined, and the weighted vote has nothing to average. The code returns a fallback or crashes on an empty neighbour list, never a real guess.
Symptom: signups get blank shelves while warm users get fine picks, and new releases sit unseen no matter how good they are. Fix: route cold rows away from this model toward onboarding picks, global populars, or content matching on genre and tags, then switch back once ratings arrive. The full routing pattern is cold-start problem.
One shared blockbuster makes strangers look identical
In a sparse table two users may share a single rated film. If both gave that one film 4 stars, their cosine over the overlap is 1.0, perfect agreement from one data point. Popular films also appear in nearly every row, so they top every neighbour list and every shelf looks the same.
Symptom: everyone gets the same top 10, and similarity 1.0 pairs disagree on everything else. Fix: demand a minimum overlap before trusting a neighbour, mean center ratings so generosity stops posing as taste, downweight mega popular items, and score the shelf with ranking checks from recommendation evaluation. When sparsity stays severe, learn dense profiles with matrix factorization instead of counting overlaps.
The Quick Version
- Collaborative filtering predicts your missing stars from rows or columns that already agree with you, reading behaviour instead of item content.
- Ana at matches Ben at 0.77 and Cara at 0.36, so Ben is the neighbour who decides.
- Ben's 5 stars for Alien transfer to Ana, which is the single predicted cell in the diagram.
- Item based agrees from the other side. Dune 1 matches Dune 2 at 0.98 and Romance at 0.39.
- Empty rows have no neighbours, and single overlap pairs fake perfect agreement, so cold rows need a fallback and sparse tables need overlap minimums or learned factors.