Item-item collaborative filtering
- Lesson 12
- 40 min
- Module 4: Neighbours and collaborative filtering
Where we are
In the last lesson, we found the neighbours of one product from the customers who touched both. That works for a product page, which shows one product. A returning customer has touched many products. In this lesson, we add up the neighbours of everything a customer touched, explain each suggestion from the data, and find the customers for whom it fails.
Lesson 12 of 29 · Recommendation and Personalisation: Show Each Person What MattersItem-item collaborative filtering
1. The recommendation problem
2. Data, implicit feedback and an honest split
3. Measure recommendations offline
4. Neighbours and collaborative filtering
5. Matrix factorisation for implicit feedback
6. Content, hybrids and cold start
7. Two stages: candidates and ranking
8. From offline scores to the real shop
9. Run it in production
What you will be able to do
Turn product neighbours into a personal list. Score every product from everything a customer touched, and explain each suggestion from the data. Then measure where item-item collaborative filtering helps Larkfield and where it fails.
NotebookDo this lesson in a notebook
The notebook scores a tiny example by hand, builds the cosine similarity of all Larkfield products, and gives each event of a customer a weight. You compute the first suggestion for one customer. Then it scores every test customer by segment and looks at how old the occasional customers’ orders are. Its setup cells download about 8 MB of practice data. About 25 minutes.
Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.
Before you startWhat to know and to have ready
What you need to know
- Cosine similarity of two products, from People also bought.
- Event weights (view, basket, order), from Interactions, not ratings.
- Recall@10 by segment and
recs compare, from Evaluate by segment.
What you need on your computer (only for the project steps)
- Your
larkfield-recsproject with the two files of the last lesson.recs similar P10020lists five armchairs. - The practice files in
../nextia-learning-labs, beside the project folder.
Tested on macOS with Python 3.12. The Windows (PowerShell) commands were not run.
The assistant who knows a regular customer
Section titled “The assistant who knows a regular customer”A regular customer walks into a garden centre. The assistant remembers her: “In spring you bought a magnolia tree and a few potted shrubs. We have new perennials that grow well under a magnolia.” She does not think about one product. She thinks about everything the customer bought, and what usually goes with each of those things. A big purchase counts more than a small one, and last month counts more than last year.
That is item-item collaborative filtering. The last lesson gave us the neighbours of one product. This lesson adds them up over a customer’s whole history.
The problem: a customer is more than one product
Section titled “The problem: a customer is more than one product”“People also bought” needs one product. A home page or an email has a customer, and a customer has a history. Customer C100123 touched eight products before the test period: a magnolia tree, six plants and a throw. Which neighbours do we show?
- The neighbours of the last product only. This ignores seven of the eight products. If the last event was a quick view, the whole list follows one click.
- The neighbours of each product, one after the other. The list fills with the first product’s neighbours. The order of the history decides the list.
- The most common neighbours by count. This brings back the bestsellers from the last lesson.
None of these uses the whole history fairly. We need one score per product that combines all the evidence.
The idea: every past product votes for its neighbours
Section titled “The idea: every past product votes for its neighbours”Item-item collaborative filtering (item-cf) gives each product a score:
score of a product = the sum, over the customer’s past products, of (weight of the customer’s event × similarity of the two products)
Each past product “votes” for its neighbours. A strong event (an order) gives a strong vote. A close neighbour gets more of the vote than a distant one. The products with the highest scores make the list. Engineers at Amazon described this design for their shop in 2003 (Linden, Smith and York, IEEE Internet Computing), and it is still common.
A tiny worked example
Section titled “A tiny worked example”Kim ordered a tent and viewed a lantern (a tiny example we made). An order has weight 5 and a view weight 1, the event weights from Module 2. The similarities are the cosines of the camping shop of the last lesson:
| Candidate | Tent (weight 5) × similarity | Lantern (weight 1) × similarity | Score |
|---|---|---|---|
| Sleeping bag | 5 × 0.707 = 3.535 | 1 × 0.408 = 0.408 | 3.943 |
| Gloves | 5 × 0.567 = 2.835 | 1 × 0.436 = 0.436 | 3.271 |
| Watering can | 5 × 0 = 0 | 1 × 0 = 0 | 0 |
The sleeping bag comes first. The tent’s vote decides it, because Kim ordered the tent and only viewed the lantern. We do not suggest the tent and the lantern themselves here. In the project, a product the customer touched can still be suggested, which is right for repeat purchases and wrong for a second tent. Business rules and re-ranking handles this.
Recent events count more
Section titled “Recent events count more”A customer’s taste changes, and seasons change. So the project halves each weight every 60 days before the test period. This is called a half-life. An order today counts 5. An order 60 days ago counts 2.5, and an order 120 days ago 1.25. A product touched several times adds up all its weights.
Larkfield: why C100123 gets a perennial
Section titled “Larkfield: why C100123 gets a perennial”The project’s recs explain command shows the sum for one customer. For C100123, the first suggestion is a perennial plant:
C100123: suggestion P10065 Silver Lamb's Ear Perennial (Outdoor plants)score 4.353 = sum of (weight of the customer's event x similarity): 2.887 = 8.11 x 0.356 P11428 White Star Magnolia Tree (Outdoor plants) 0.411 = 0.90 x 0.456 P11784 Blueberry Shrub in Oak Pot (Outdoor plants) 0.342 = 0.90 x 0.379 P11786 Climbing Clematis in Blue Pot (Outdoor plants) 0.320 = 0.90 x 0.355 P11930 Compact Boxwood Shrub in Black Pot (Outdoor plants) 0.270 = 0.90 x 0.300 P12226 Hydrangea in Terracotta Pot (Outdoor plants) 0.124 = 0.90 x 0.138 P12362 Purple Coneflower in Mustard Pot (Outdoor plants)Text description of the diagram
On the left is the history of customer C100123: the White Star Magnolia Tree with weight 8.11, and five potted plants with weight 0.90 each. Arrows lead to the first suggestion, the Silver Lamb’s Ear Perennial, with a score of 4.353. The magnolia tree adds 8.11 × 0.356 = 2.887. The blueberry shrub adds 0.90 × 0.456 = 0.411, the clematis 0.90 × 0.379 = 0.342, and the three other plants 0.320, 0.270 and 0.124.
The magnolia tree has a weight of 8.11, more than one order (5). The customer touched it several times, recently. It gives two thirds of the score. The five potted plants have small weights (0.90): old or weak events. They add a little each. Every number in the explanation comes from the data, so Hana or Omar can check it.
The notebook shows the whole history. The customer also viewed the perennial itself (weight 0.90), without buying it. A product never counts as its own neighbour, so that view adds nothing to the score. But the project does not remove products the customer already touched. Here that is reasonable: she looked at the perennial and did not buy it yet.
Why item-item is stable and easy to explain
Section titled “Why item-item is stable and easy to explain”Easy to explain. Every suggestion is a sum of a few products that the customer knows. A page can say “because you bought a White Star Magnolia Tree”. Customers trust a reason they can see, and the team can find a bad suggestion fast: which past product brought it in?
Stable. A product’s neighbours come from many customers. An armchair touched by a hundred customers does not change its neighbours when one more customer arrives. So the neighbour table can be computed once a night and kept. When we train the model one week earlier, 84% of the products in a customer’s list stay the same. A new event of the customer changes only their own weights, not the table.
Cheap to serve. The table has at most 50 neighbours for each of 2,994 products. Computing it takes 0.4 seconds on one thread. Lists for all 40,000 customers take 0.8 seconds more.
Larkfield: item-cf on the test period
Section titled “Larkfield: item-cf on the test period”Now the honest result. On Larkfield’s test period, item-cf finds 0.088 of the ordered products (95% interval 0.077 to 0.101). That is below trending (0.156) and below the count method cooc (0.122). Split by segment:
- trending
- cooc
- item-cf
Text description of the chart
Three panels, one per segment, each with a horizontal axis for recall@10 from 0 to 0.2. New customers (952): trending 0.169, cooc 0.137, item-cf 0.127. Occasional customers (556): trending 0.162, cooc 0.097, item-cf 0.045. Loyal customers (610): trending 0.132, cooc 0.121, item-cf 0.067. Item-cf is the lowest of the three in every segment.
Item-cf is the lowest of the three in every segment. On the same customers, recs compare trending item-cf says that item-cf is better for 99 customers and worse for 284.
The new customers’ score is mostly trending
Section titled “The new customers’ score is mostly trending”Item-cf gets 0.127 for new customers. That looks good until you split them. Of the 952 new test customers, 633 touched nothing before the period. Item-cf has nothing to score for them, so the trending list fills their places. They get 0.168, which is trending’s score. The 319 new customers who viewed something get only 0.046 from item-cf. Trending gives the same customers 0.172.
So a fallback can make a weak method look fine in a segment. Always ask: whose list did the method really make?
Why item-cf is weak here: most customers buy once
Section titled “Why item-cf is weak here: most customers buy once”Item-cf needs a history, and a history that points at what the customer wants next. Larkfield’s customers rarely have one.
Text description of the chart
A column chart of the 23,809 customers who ordered at least once, by number of orders. 1 order: 12,783 customers. 2 orders: 4,838. 3 orders: 2,296. 4 orders: 1,279. 5 orders: 773. 6 or more: 1,840. The first column is taller than all the others together.
53.7% of Larkfield’s buyers ordered once. For an occasional customer, the history is one or two orders. The notebook finds that their last order is a median of 38.5 days before the test period, about five weeks. Item-cf suggests the neighbours of that order. As the armchair showed, many neighbours are alternatives: other armchairs for a customer who has just bought one. The customer needs them no longer. That is why item-cf gets only 0.045 for occasional customers.
Cosine also lowers the weight of popular products, as the last lesson showed. In a shop where in-season bestsellers sell well, that costs recall: the count method cooc keeps more bestsellers and scores 0.122.
Where item-cf helps: the product page
Section titled “Where item-cf helps: the product page”Item-cf answers one question very well: “what does a customer look at next, after this product?” Sessions, sequences and LLMs, briefly measures it on the test-period visits. In 16,801 moves from one product to the next, the next product was in the current product’s top 10 item-cf neighbours 62% of the time. Trending’s list held it only 2.7% of the time.
Text description of the chart
A column chart with two columns. The top 10 item-cf neighbours of the current product contain the next product viewed in the same visit 62.0% of the time. The trending list contains it 2.7% of the time. The measure uses 16,801 moves in the visits of the test period.
So the same method is weak on the home page and strong on the product page. The difference is the signal. On the product page, the customer is looking at the product now. On the home page, item-cf works from earlier orders, and their neighbours are often things the customer has already bought.
When to use it, when not, and what it costs
Section titled “When to use it, when not, and what it costs”Use item-cf for “people also bought” and “similar products” rows on product pages, and for the basket page. Use it when you need a reason you can show (“because you bought …”). Use it as one source of candidates among several, as Why two stages does.
Do not use it alone for a home page or an email when most customers buy once, as at Larkfield. Check a fallback segment apart, as we did with the new customers. Do not use it for products with no history: a new product has no neighbours until customers touch it.
Cost: fit 0.4 seconds and about 609 MB at peak on one thread; lists for 40,000 customers in 0.8 seconds. The neighbour table can be rebuilt every night.
Guided practice: explain a suggestion
Section titled “Guided practice: explain a suggestion”-
Open the notebook from the box Do this lesson in a notebook at the top of this page. Run the setup cells.
-
In section 1, predict whether Kim gets the sleeping bag or the gloves first. Then run the cell.
-
Run sections 2 and 3. Section 3 prints C100123’s history with its weights. The magnolia tree has the largest weight, 8.11.
-
In section 4, compute the scores and the first suggestion. The check prints
Check passed: top is right. -
Run sections 5 and 6. You see item-cf’s recall by segment, and how old the occasional customers’ last orders are.
-
In a terminal, in your
larkfield-recsfolder with the virtual environment active, copy theexplaincommand:Terminal window S=../nextia-learning-labs/C37/snapshots/end-of-m04cp $S/larkfield_recs/commands/explain.py larkfield_recs/commands/Terminal window $S = "..\nextia-learning-labs\C37\snapshots\end-of-m04"Copy-Item "$S\larkfield_recs\commands\explain.py" larkfield_recs\commands\ -
Score item-cf by segment:
Terminal window recs evaluate item-cf --by segmentMethod item-cf (item-item collaborative filtering (cosine, 50 neighbours, recency)), test periodCustomers scored: 2,118; trained on events before 2026-06-22recall@10 0.088 (95% interval 0.077 to 0.101), NDCG@10 0.055, MAP@10 0.039, precision@10 0.014, hit rate 11.9%coverage 53.2% of the products on sale, novelty 6.45 bits, diversity 0.44, new products in the lists 11.5%time: fit 0.3 s, suggestions 0.1 ssegment customers recall@10 NDCG@10 95% interval (recall)new 952 0.127 0.082 0.107 to 0.148occasional 556 0.045 0.026 0.030 to 0.061loyal 610 0.067 0.040 0.050 to 0.086new products: 647 orders of new products in the period; recall@10 on them 0.128 -
Explain the first suggestion for C100123:
Terminal window recs explain C100123You see the explanation from the section above:
score 4.353, and the magnolia tree first with2.887 = 8.11 x 0.356. -
Compare item-cf with the trending baseline. You evaluated
trendingin Module 3; ifoutputs/runs/is empty, runrecs evaluate trendingfirst.Terminal window recs compare trending item-cf --by segmentrecall@10: trending 0.156, item-cf 0.088difference -0.068 (95% interval -0.083 to -0.055); better for 99 customers, worse for 284, the same for 1,735The difference is clear: the interval does not include 0.coverage 0.3% -> 53.2%; novelty 5.10 -> 6.45new 952 customers -0.042 (-0.058 to -0.026)occasional 556 customers -0.117 (-0.150 to -0.083)loyal 610 customers -0.065 (-0.096 to -0.036)
Show the answer to step 4
scores = H[cpos["C100123"]] @ simscores = np.asarray(scores).ravel()top = pid[np.argmax(scores)]One row of H holds the customer’s weights. Multiplying it by the similarity matrix adds up “weight × similarity” for every product at once. The highest score belongs to P10065, the Silver Lamb’s Ear Perennial, as recs explain prints.
Your turn: counts or cosine?
Section titled “Your turn: counts or cosine?”Change one thing: the similarity. cooc is the same method with counts instead of cosine. Before you run it, predict: for which segment will counts beat cosine by the most? Then run recs compare cooc item-cf --by segment. If you have no saved run of cooc, run recs evaluate cooc first.
Show the answer
recall@10: cooc 0.122, item-cf 0.088difference -0.034 (95% interval -0.042 to -0.025); better for 42 customers, worse for 134, the same for 1,942The difference is clear: the interval does not include 0.coverage 36.2% -> 53.2%; novelty 5.55 -> 6.45 new 952 customers -0.010 (-0.017 to -0.003) occasional 556 customers -0.052 (-0.074 to -0.031) loyal 610 customers -0.053 (-0.076 to -0.034)Counts beat cosine by the most for occasional and loyal customers (−0.052 and −0.053). Those are the customers with a history, so the similarity really matters for them. Counts keep more bestsellers, and bestsellers sell. Cosine shows more of the shop (coverage 53.2% against 36.2%). That is the trade-off of the last lesson, now measured on the home page. Note that the order for one product page can be the other way round: there, the product on the screen is a strong signal.
Common mistakes
Section titled “Common mistakes”Common mistake
A segment looks fine because the fallback did the work
Symptom: Tomás reports that item-cf works “well for new customers: 0.127, almost as good as trending”.
Cause: 633 of the 952 new customers touched nothing before the period. Item-cf made no list for them, and the trending fallback filled every place. The segment’s score is mostly trending’s.
Diagnose: split the segment by whether the customer has any event. Item-cf’s own lists, for new customers with views, get 0.046.
Fix: report the fallback’s share beside the score, or score the method only on the customers it really served.
Common mistake
Higher recall, and a winter coat in late June
Symptom: Tomás wants to replace item-cf with cooc, because its recall is higher: 0.122 against 0.088. Rafael opens a few cooc lists of the test period. Many of them suggest a winter coat at the end of June.
Cause: a duffle coat is one of the shop’s bestsellers over the whole log: 857 buyers, and no order in the test period. It is a practice problem we made on purpose. Counts favour bestsellers, so the coat becomes a neighbour of many products. 20% of the cooc lists contain a winter coat, and 48% of the loyal customers’ lists. With cosine, only 0.5% of the item-cf lists do.
Diagnose: look at the lists, not only at the score. Count the lists with an out-of-season product.
Fix: a higher recall does not make a list good. Prefer cosine, and add a season rule for products that do not sell in the period. Business rules and re-ranking adds them.
Knowledge check
Section titled “Knowledge check”Check your understanding
A customer ordered a lamp (weight 5) and viewed a rug (weight 1). A cushion has similarity 0.2 to the lamp and 0.6 to the rug. A side table has similarity 0.3 to the lamp and 0 to the rug. Which one does item-cf rank first?
Show the answer
Cushion: 5 × 0.2 + 1 × 0.6 = 1.6. Side table: 5 × 0.3 + 1 × 0 = 1.5. The cushion comes first, but only just. The strong order of the lamp almost wins for the side table; the view of the rug decides it.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Why are item-item neighbours more stable than the list of one customer's similar customers?
Show the answer
A product’s neighbours come from all the customers who touched it, often dozens or hundreds. One new customer or one new event changes them very little, so the table can be computed once a night. A customer’s own history is short, often one or two orders, so their nearest customers can change with one new event. On Larkfield, 84% of an item-cf list stays the same when the model is trained one week earlier. The next lesson measures the customer side.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Item-cf has recall@10 0.088 on the home page, but its neighbours contain the next viewed product 62% of the time on a product page. Explain the difference in one or two sentences.
Show the answer
On a product page, the product on the screen shows what the customer wants now, and its neighbours are the products customers compare it with. On the home page, item-cf works from the customer’s past orders. At Larkfield these are usually one or two orders, and their neighbours are often alternatives that the customer no longer needs.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Recap
- Item-item collaborative filtering scores a product by the sum of (weight of the customer’s event × similarity) over the customer’s past products. Events are view 1, basket 3, order 5, halved every 60 days.
- It is easy to explain: in
recs explain C100123, the magnolia tree adds 2.887 of the score 4.353. It is stable: a product’s neighbours come from many customers. - On Larkfield’s test period it gets 0.088, below trending (0.156) and counts (0.122). It is very weak for occasional customers (0.045): their one or two orders point at alternatives they no longer need.
- Its new-customer score is mostly the trending fallback: new customers with views get 0.046.
- It is strong on the product page: the next viewed product is in the current product’s neighbours 62% of the time.
Item-item looks for similar products. We can also look for similar customers and borrow what they bought. Next, we try user-user collaborative filtering, and measure why it is slow and fragile in a shop where most customers buy once.
Next: User-user and its limits