Skip to content
AI360XpertBeta

Collaborative Filtering

You get picks from people whose past ratings match yours, not from item descriptions. Overlap in behavior decides the next recommendation.

Ana matches Ben at 0.77 against 0.36 for Cara, so Ben's 5 stars for Alien become Ana's recommendation
Ana matches Ben at 0.77 against 0.36 for Cara, so Ben's 5 stars for Alien become Ana's 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 RR, with mm users as rows and nn items as columns. Entry RijR_{ij} is the stars user ii gave item jj, 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 rur_u for one user row, meaning the full list of that user's stars across all nn items. Write sim(u,v)\text{sim}(u, v) for the similarity between users uu and vv, a number where larger means closer taste. With cosine similarity, that number is the dot product scaled by both lengths:

sim(u,v)=ru⋅rv∥ru∥ ∥rv∥\text{sim}(u, v) = \frac{r_u \cdot r_v}{\lVert r_u \rVert \, \lVert r_v \rVert}

Here ru⋅rvr_u \cdot r_v multiplies matching entries and adds them, and ∥ru∥\lVert r_u \rVert 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 kk closest users who rated the target item and average their stars weighted by sim\text{sim}. 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: [5,4,0,0][5, 4, 0, 0] for Dune 1, Dune 2, Romance, Alien
  • Ben: [5,4,2,5][5, 4, 2, 5]
  • Cara: [1,2,5,1][1, 2, 5, 1]

Ana's length covers two ratings:

∥rAna∥=25+16=41≈6.40\lVert r_{\text{Ana}} \rVert = \sqrt{25 + 16} = \sqrt{41} \approx 6.40

Ben's length covers four:

∥rBen∥=25+16+4+25=70≈8.37\lVert r_{\text{Ben}} \rVert = \sqrt{25 + 16 + 4 + 25} = \sqrt{70} \approx 8.37

Their dot counts only the shared signal:

rAna⋅rBen=25+16+0+0=41r_{\text{Ana}} \cdot r_{\text{Ben}} = 25 + 16 + 0 + 0 = 41

So:

sim(Ana,Ben)=416.40×8.37≈4153.57≈0.77\text{sim}(\text{Ana}, \text{Ben}) = \frac{41}{6.40 \times 8.37} \approx \frac{41}{53.57} \approx 0.77

Cara's length is 1+4+25+1=31≈5.57\sqrt{1 + 4 + 25 + 1} = \sqrt{31} \approx 5.57. Her dot with Ana is 5+8+0+0=135 + 8 + 0 + 0 = 13, and 6.40×5.57≈35.656.40 \times 5.57 \approx 35.65, so:

sim(Ana,Cara)=1335.65≈0.36\text{sim}(\text{Ana}, \text{Cara}) = \frac{13}{35.65} \approx 0.36

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 [5,5,1][5, 5, 1] down Ana, Ben, Cara. Dune 2 is [4,4,2][4, 4, 2]. Romance is [0,2,5][0, 2, 5].

sim(Dune 1,Dune 2)=427.14×6.00≈4242.85≈0.98\text{sim}(\text{Dune 1}, \text{Dune 2}) = \frac{42}{7.14 \times 6.00} \approx \frac{42}{42.85} \approx 0.98 sim(Dune 1,Romance)=157.14×5.39≈1538.46≈0.39\text{sim}(\text{Dune 1}, \text{Romance}) = \frac{15}{7.14 \times 5.39} \approx \frac{15}{38.46} \approx 0.39

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 stars

The 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 [5,4,0,0][5, 4, 0, 0] 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.