Skip to content
Français

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.

Plays from YouTube. Nothing loads from YouTube until you press play. Privacy

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 notebookColab · Kaggle · GitHub

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 readyAbout 40 minutes

What you need to know

What you need on your computer (only for the project steps)

  • Your larkfield-recs project with the two files of the last lesson. recs similar P10020 lists 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.

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.

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.

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)
C100123's history on the left: White Star Magnolia Tree with weight 8.11, Blueberry Shrub in Oak Pot 0.90, Climbing Clematis in Blue Pot 0.90, and 3 more potted plants at 0.90 each. Arrows lead to the suggestion on the right, Silver Lamb's Ear Perennial, score 4.353. The arrows are labelled: 8.11 × 0.356 = 2.887; 0.90 × 0.456 = 0.411; 0.90 × 0.379 = 0.342; and 0.320 + 0.270 + 0.124 for the three other plants.
One suggestion, explained. Each product in the customer's history adds its weight times its similarity to the suggestion; the magnolia tree adds two thirds of the score.
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.

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
Recall@10 of trending, cooc and item-cf in each customer segment of the test period, with 95% intervals. Item-cf is lowest in every segment, and very low for occasional customers.
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.

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.

Orders per buyer over the whole shop log (synthetic). More than half of the customers who ordered did so only once.
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.

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.

On a product page, item-cf neighbours of the current product contain the next product the customer viewed 62% of the time; the trending list 2.7% of the time. Test-period visits, 16,801 moves.
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.

  1. Open the notebook from the box Do this lesson in a notebook at the top of this page. Run the setup cells.

  2. In section 1, predict whether Kim gets the sleeping bag or the gloves first. Then run the cell.

  3. Run sections 2 and 3. Section 3 prints C100123’s history with its weights. The magnolia tree has the largest weight, 8.11.

  4. In section 4, compute the scores and the first suggestion. The check prints Check passed: top is right.

  5. Run sections 5 and 6. You see item-cf’s recall by segment, and how old the occasional customers’ last orders are.

  6. In a terminal, in your larkfield-recs folder with the virtual environment active, copy the explain command:

    Terminal window
    S=../nextia-learning-labs/C37/snapshots/end-of-m04
    cp $S/larkfield_recs/commands/explain.py larkfield_recs/commands/
  7. Score item-cf by segment:

    Terminal window
    recs evaluate item-cf --by segment
    Method item-cf (item-item collaborative filtering (cosine, 50 neighbours, recency)), test period
    Customers scored: 2,118; trained on events before 2026-06-22
    recall@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 s
    segment customers recall@10 NDCG@10 95% interval (recall)
    new 952 0.127 0.082 0.107 to 0.148
    occasional 556 0.045 0.026 0.030 to 0.061
    loyal 610 0.067 0.040 0.050 to 0.086
    new products: 647 orders of new products in the period; recall@10 on them 0.128
  8. Explain the first suggestion for C100123:

    Terminal window
    recs explain C100123

    You see the explanation from the section above: score 4.353, and the magnolia tree first with 2.887 = 8.11 x 0.356.

  9. Compare item-cf with the trending baseline. You evaluated trending in Module 3; if outputs/runs/ is empty, run recs evaluate trending first.

    Terminal window
    recs compare trending item-cf --by segment
    recall@10: trending 0.156, item-cf 0.088
    difference -0.068 (95% interval -0.083 to -0.055); better for 99 customers, worse for 284, the same for 1,735
    The difference is clear: the interval does not include 0.
    coverage 0.3% -> 53.2%; novelty 5.10 -> 6.45
    new 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"]] @ sim
scores = 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.

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.088
difference -0.034 (95% interval -0.042 to -0.025); better for 42 customers, worse for 134, the same for 1,942
The 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 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.

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.

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.

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.

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