Skip to content
Français

People also bought

  • Lesson 11
  • 35 min
  • Module 4: Neighbours and collaborative filtering

Where we are

In Module 3, we built a harness that measures any method, by segment, and compares two methods on the same customers. Every method so far shows the same products to everybody. In this lesson, we try the oldest personal idea in shops: count which products the same customers buy, and stop bestsellers from appearing next to everything.

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

What you will be able to do

Build “people also bought” from the shop log. Count the customers who bought two products. Then correct the count with lift or cosine, so that the suggestions next to a product are not only the shop’s bestsellers.

NotebookDo this lesson in a notebookColab · Kaggle · GitHub

The notebook counts the products touched together by the same customers, and ranks the neighbours of one armchair three ways. You find the closest product to a magnolia tree. Then it measures how popular each kind of neighbour is. Its setup cells download about 8 MB of practice data. About 20 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 35 minutes

What you need to know

What you need on your computer (only for the project steps; the notebook needs none of it)

  • Your larkfield-recs project at the end of Module 3, with its virtual environment active. recs check prints Setup check passed.
  • The practice files in ../nextia-learning-labs, beside the project folder.

Tested on macOS with Python 3.12, NumPy 2.5.4, pandas 3.1.1 and SciPy 1.18.1. The Windows (PowerShell) commands were not run.

You buy a tent in a small camping shop. The assistant says: “Most people who buy this tent also take a sleeping bag. Do you have one?” She does not know you. She remembers what other customers put in their baskets together with this tent.

That is “people also bought”. It needs no knowledge of the customer, only of the product on the screen. It is the oldest personal suggestion in shops, and it is easy to explain: “customers who bought this also bought that”.

Now imagine a new assistant who learns the same rule by counting. Every customer buys garden gloves, because they are cheap and useful. So gloves were in many baskets with the tent. The new assistant says: “People who buy this tent also buy gloves.” It is true, and it is useless. He would say the same next to a sofa, a lamp or a cushion.

The problem: counting puts bestsellers everywhere

Section titled “The problem: counting puts bestsellers everywhere”

A co-occurrence count is the number of customers who touched both products. “Touched” means any event in the log: a view, a basket add or an order. We count each customer once for each pair.

Counts favour bestsellers. A product that 40% of customers touch is in 40% of every product’s baskets, by chance alone. So the count list of almost every product starts with the same few popular products.

Larkfield’s log shows it. The notebook takes 300 products and their 5 best neighbours by count. Those neighbours were touched by 428 customers on average. A typical Larkfield product was touched by 103 customers (the median). So the count list leans strongly to popular products. We need a measure that asks a different question: “is this pair more common than chance would make it?”

Two corrections are common. Both start from the count and divide by the popularity of the products.

Here are eight customers and five products of a camping shop. It is a tiny example we made, so that every number is visible.

Customer Tent Sleeping bag Lantern Gloves Watering can
Ana ✓ ✓ ✓
Ben ✓ ✓ ✓
Chen ✓ ✓
Dara ✓ ✓
Eli ✓ ✓
Femi ✓
Gus ✓ ✓ ✓
Ivo ✓ ✓
Customers (n) 4 2 3 7 2

Seven of the eight customers bought gloves. That is the bestseller. We want the suggestions for the tent’s page.

Count the customers who bought the tent and each other product:

With the tent Shared customers
Gloves 3 (Ana, Chen, Gus)
Sleeping bag 2 (Ana, Ben)
Lantern 2 (Ben, Gus)
Watering can 0

By count, gloves come first. That is the new assistant’s mistake.

Lift compares the shared customers with the number that chance would give. Suppose two products have nothing to do with each other. Then the share of customers who buy both is the share who buy the first, times the share who buy the second. Lift is the real share divided by that chance share:

lift = shared × N / (n₁ × n₂)

N is the number of customers (8), and n₁ and n₂ are the customers of each product. For the tent and the sleeping bag: 2 × 8 / (4 × 2) = 2.0. The pair is twice as common as chance. For the tent and the gloves: 3 × 8 / (4 × 7) = 0.86. That is less than chance. Tent buyers buy gloves a little less often than other customers.

A lift above 1 means “bought together more than by chance”. A lift of 1 means “no link”. Below 1 means “less than by chance”.

Cosine similarity divides the shared customers by the square root of the two products’ customer counts:

cosine = shared / √(n₁ × n₂)

Tent and sleeping bag: 2 / √(4 × 2) = 0.707. Tent and gloves: 3 / √(4 × 7) = 0.567. The name comes from geometry. Each product is a column of the table, a list of 0s and 1s. The cosine measures the angle between two columns. Two products bought by exactly the same customers get 1. Two products with no shared customer get 0.

Cosine punishes popularity less than lift. Lift divides by n₂, cosine only by √n₂. So a bestseller with a strong link can still rank well by cosine.

The tiny basket table turned into suggestions for the tent's page. Shared customers with the tent: gloves 3, sleeping bag 2, lantern 2, watering can 0. By count the order is gloves 3, sleeping bag 2, lantern 2. By lift: sleeping bag 2.0, lantern 1.33, gloves 0.86. By cosine: sleeping bag 0.707, lantern 0.577, gloves 0.567. Lift and cosine both lead to the suggestion: sleeping bag, then lantern.
The same shared customers, ranked three ways. Count puts the bestseller first; lift and cosine put the sleeping bag first.
Text description of the diagram

The diagram starts from the basket table: the tent has 4 customers, the gloves 7, the sleeping bag 2 and the lantern 3. A box lists the shared customers with the tent: gloves 3, sleeping bag 2, lantern 2, watering can 0. Three arrows rank them. Count: gloves 3, sleeping bag 2, lantern 2. Lift, shared times 8 divided by the two counts: sleeping bag 2.0, lantern 1.33, gloves 0.86. Cosine, shared divided by the square root of the two counts: sleeping bag 0.707, lantern 0.577, gloves 0.567. Lift and cosine lead to the same suggestion on the tent’s page: the sleeping bag, then the lantern.

Count, lift and cosine use the same table. They only divide by different things. Count divides by nothing, so popularity wins. Lift divides by both counts, so rare products win. Cosine is between the two.

Now the real log. In this course, a product’s neighbours are the products with the highest similarity to it. The project’s recs similar command lists them, with the number of shared customers. Here is the Driftwood Blush Pink Wingback Armchair, P10020, by cosine:

P10020 Driftwood Blush Pink Wingback Armchair (Armchairs, 432.99 dollars)
1. P12928 0.189 Driftwood Terracotta Rattan Armchair (Armchairs) shared customers 17
2. P12206 0.172 Red Coastal Adirondack-Style Armchair (Armchairs) shared customers 7
3. P10478 0.154 Mustard Tufted Classic Club Armchair (Armchairs) shared customers 3
4. P10682 0.149 Black Rustic Leather Sling Armchair (Armchairs) shared customers 10
5. P11410 0.148 Red Coastal Striped Occasional Armchair (Armchairs) shared customers 4
Product P10020, the Driftwood Blush Pink Wingback Armchair, with arrows to its five nearest products by cosine: P12928 Driftwood Terracotta Rattan Armchair, 0.189, 17 shared customers; P12206 Red Coastal Adirondack-Style Armchair, 0.172, 7 shared; P10478 Mustard Tufted Classic Club Armchair, 0.154, 3 shared; P10682 Black Rustic Leather Sling Armchair, 0.149, 10 shared; P11410 Red Coastal Striped Occasional Armchair, 0.148, 4 shared.
The five nearest products of one armchair, by cosine, from the customers who touched both before the test period. All five are armchairs; the first is from the same range.
Text description of the diagram

The armchair P10020 is in the middle box. Five arrows lead to its nearest products. Each arrow shows the cosine and the shared customers. P12928, Driftwood Terracotta Rattan Armchair: 0.189 and 17 shared customers. P12206, Red Coastal Adirondack-Style Armchair: 0.172 and 7. P10478, Mustard Tufted Classic Club Armchair: 0.154 and 3. P10682, Black Rustic Leather Sling Armchair: 0.149 and 10. P11410, Red Coastal Striped Occasional Armchair: 0.148 and 4.

All five neighbours are armchairs. The first one is from the same range, Driftwood, with 17 shared customers. Nobody told the method what an armchair is. It found them because customers who look at one armchair look at other armchairs. That is why this family of methods is called collaborative filtering: the customers’ behaviour, taken together, does the filtering.

These are mostly alternatives, not complements. An alternative is a product you buy instead (another armchair). A complement is a product you buy with it (a cushion for the armchair). Views make alternatives strong, because customers compare several armchairs before they buy one. On a product page, both are useful. In a weekly email, a second armchair is rarely what a customer wants a week after buying one.

The same armchair by lift:

1. P10478 304.579 Mustard Tufted Classic Club Armchair (Armchairs) shared customers 3
2. P11410 208.854 Red Coastal Striped Occasional Armchair (Armchairs) shared customers 4
3. P12206 161.928 Red Coastal Adirondack-Style Armchair (Armchairs) shared customers 7

The first neighbour by lift has only 3 shared customers. Its lift is huge because few customers touched it at all. Three customers are thin evidence: one more or one less customer changes the lift a lot. The project already ignores pairs with only one shared customer, because one customer is not evidence. Lift still trusts small numbers.

The notebook repeats the armchair test on 300 products and measures how many customers touched their 5 best neighbours:

How popular the top 5 neighbours of 300 products are, by count, cosine and lift, and the median product for comparison. Counts lean to bestsellers, lift to rare products.
Text description of the chart

A column chart with four columns. The top 5 neighbours by count were touched by 428 customers on average. By cosine: 286. By lift: 68. The fourth column is the median product of the shop, touched by 103 customers. Count picks the most popular neighbours, lift the least popular, and cosine is between them.

Count picks the most popular neighbours, as in the camping shop. Lift swings to the other side and picks rare products. Cosine is between. This is why the project’s main item method uses cosine.

A product page knows the product on the screen. A home page knows the customer. To use neighbours on a home page, we take the products a customer touched before and suggest their neighbours. The next lesson builds this properly, as item-item collaborative filtering. The project already runs the count and lift versions as methods, cooc and lift, so we can put them in the comparison table now.

Recall@10 of the two co-occurrence methods and the trending baseline on Larkfield's test period, with 95% intervals over the 2,118 test customers.
Text description of the chart

A plot with one row per method and a horizontal axis for recall@10 from 0 to 0.2. Trending: 0.156, interval 0.143 to 0.172. Cooc, the count method: 0.122, interval 0.109 to 0.136. Lift: 0.059, interval 0.049 to 0.069. Neither interval overlaps with trending’s.

Both are below trending on Larkfield’s test period. The count method gets 0.122: its popular neighbours often are in-season bestsellers, which customers buy. Lift gets 0.059. Lift does best for new customers (0.116), who often have only a few views of this season’s products. For occasional and loyal customers, lift collapses to 0.010 and 0.014: it fills their lists with rare neighbours of old purchases.

Lift has one strength: coverage. Its lists show 74.5% of the products on sale, against 36.2% for counts and 0.3% for trending. Rare products get a chance. The next lesson shows how cosine with recency weights sits between the two, and why it still does not beat trending here.

When to use it, when not, and what it costs

Section titled “When to use it, when not, and what it costs”

Use co-occurrence on a product page, where the product on the screen is a strong signal. Use cosine (or lift with a minimum number of shared customers) rather than raw counts. Show the shared-customer number to the merchandising team: “17 customers bought both” is easy to trust.

Do not use raw counts if you care about the long tail: every page shows the same bestsellers. Do not use lift without a minimum count: pairs with 2 or 3 shared customers win. And do not expect neighbours alone to make a good home page for customers who buy once. Their history is one or two products.

Cost: the project computes all pairs from 467,160 customer-product cells in about 0.3 seconds on one thread, and keeps 50 neighbours per product. The table of neighbours is small (2,994 products × 50). A product page only looks it up.

Guided practice: neighbours in your project

Section titled “Guided practice: neighbours in your project”
  1. Open the notebook from the box Do this lesson in a notebook at the top of this page. Run the setup cells.

  2. Run section 1. It prints the size of the product-by-product count table.

  3. In section 2, predict which list has the most armchairs. Then run the cell: count, lift and cosine lists for P10020.

  4. In section 3, find the closest product to P11428 by cosine. The check prints Check passed: best is right.

  5. Run section 4. You see the average popularity of the neighbours: about 428 by count, 286 by cosine and 68 by lift.

  6. In a terminal, go to your larkfield-recs folder and activate its virtual environment. Copy the neighbour methods and the similar command:

    Terminal window
    S=../nextia-learning-labs/C37/snapshots/end-of-m04
    cp $S/larkfield_recs/methods/neighbours.py larkfield_recs/methods/
    cp $S/larkfield_recs/commands/similar.py larkfield_recs/commands/
  7. List the methods. Among the methods you know, four new lines appear:

    Terminal window
    recs evaluate --list
    cooc people also bought: products bought by the same customers (counts)
    lift people also bought, by lift: shared customers against chance
    item-cf item-item collaborative filtering (cosine, 50 neighbours, recency)
    user-user user-user collaborative filtering (cosine, 50 similar customers)
  8. Show the neighbours of the armchair by count:

    Terminal window
    recs similar P10020 --method cooc --n 5
    P10020 Driftwood Blush Pink Wingback Armchair (Armchairs, 432.99 dollars)
    1. P12928 17.000 Driftwood Terracotta Rattan Armchair (Armchairs) shared customers 17
    2. P12346 10.000 Birka Grey Nordic Basket-Weave Armchair (Armchairs) shared customers 10
    3. P10682 10.000 Black Rustic Leather Sling Armchair (Armchairs) shared customers 10
    4. P11564 8.000 Grey Nordic Oversized Lounge Armchair (Armchairs) shared customers 8
    5. P10164 8.000 Black Metal-Frame Industrial Armchair (Armchairs) shared customers 8
  9. Run it again with --method item-cf, which is cosine. You see the list from the section above, with P12928 first at 0.189.

  10. Score the count method by segment:

    Terminal window
    recs evaluate cooc --by segment
    Method cooc (people also bought: products bought by the same customers (counts)), test period
    Customers scored: 2,118; trained on events before 2026-06-22
    recall@10 0.122 (95% interval 0.109 to 0.136), NDCG@10 0.072, MAP@10 0.050, precision@10 0.019, hit rate 16.6%
    coverage 36.2% of the products on sale, novelty 5.55 bits, diversity 0.72, new products in the lists 9.3%
    time: fit 0.3 s, suggestions 0.1 s
    segment customers recall@10 NDCG@10 95% interval (recall)
    new 952 0.137 0.086 0.116 to 0.158
    occasional 556 0.097 0.051 0.075 to 0.121
    loyal 610 0.121 0.071 0.098 to 0.144
    new products: 647 orders of new products in the period; recall@10 on them 0.116
Show the answer to step 4
best = neighbours("P11428", "cosine", k=1)[0]

neighbours already computes the three measures. You only ask for the cosine list and keep its first product. P11428 is the White Star Magnolia Tree. You meet it again in the next lesson, in the history of customer C100123.

The armchair’s count list is all armchairs too. For this product, counts work: armchairs are not bestsellers, so no single popular product takes over. The bestseller effect shows on average, over many products, as in the chart above.

Change one thing: the method. Score lift by segment. Before you run it, predict: will lift be better or worse than cooc for loyal customers? Then run recs evaluate lift --by segment.

Show the answer
recall@10 0.059 (95% interval 0.049 to 0.069), NDCG@10 0.038, MAP@10 0.028, precision@10 0.010, hit rate 7.9%
coverage 74.5% of the products on sale, novelty 8.25 bits, diversity 0.41, new products in the lists 24.0%
segment customers recall@10 NDCG@10 95% interval (recall)
new 952 0.116 0.076 0.098 to 0.136
occasional 556 0.010 0.006 0.004 to 0.017
loyal 610 0.014 0.008 0.008 to 0.022

Much worse: 0.014 for loyal customers, against 0.121 with counts. Loyal customers have long histories, and lift turns every old product into a list of rare neighbours. Novelty is high (8.25 bits) and coverage is high (74.5%). A list can be new and varied and still help nobody.

Common mistake

The same bestsellers on every product page

Symptom: Hana opens ten product pages. The “people also bought” row shows the same few bestsellers on almost all of them. In late June, one of them is a winter coat.

Cause: the row is ranked by raw co-occurrence counts. A product that many customers touch is in many baskets by chance, so it tops the count list of almost every product. A duffle coat with 857 buyers over the whole log is such a product: a practice problem we made on purpose. On the home page, 20.0% of the cooc lists of the test period contain a winter coat.

Fix: rank by cosine, or by lift with a minimum number of shared customers. Check the fix with the bestseller test of the notebook: the average popularity of the neighbours should fall.

Common mistake

A strange product at the top of the lift list

Symptom: the lift list of an armchair starts with a product that only 3 customers ever touched together with it. Hana asks why.

Cause: lift divides by the popularity of both products. A rare product gets a huge lift from a handful of shared customers.

Diagnose: recs similar P10020 --method lift prints the shared customers beside each lift.

Fix: use cosine, or require more shared customers before you trust a lift. The project requires at least 2; a product page may need 5 or 10.

Check your understanding

In a shop of 100 customers, 20 bought a barbecue, 50 bought garden gloves, and 10 bought both. Compute the lift and the cosine of the pair. Is the pair bought together more than by chance?

Show the answer

Lift = 10 × 100 / (20 × 50) = 1.0. Cosine = 10 / √(20 × 50) = 10 / 31.6 = 0.316. A lift of 1 means no link: half of the barbecue buyers bought gloves, and half of all customers bought gloves. The count of 10 looks big only because gloves are popular.

Check your understanding

Why does a ranking by raw co-occurrence counts put the same few products on almost every product page?

Show the answer

A count is the number of customers who touched both products. A product that many customers touch shares many customers with every other product, just by chance. So the bestsellers top the count list of almost every product. On Larkfield, the top 5 count neighbours of 300 products were touched by 428 customers on average, against 286 by cosine. Lift and cosine divide by the popularity of the products to correct for this.

Check your understanding

Lift gives the widest coverage (74.5%) and the highest novelty of the three. Tomás says it is therefore the best method for the home page. What do you answer?

Show the answer

Coverage and novelty say that the lists are varied and unusual, not that they are useful. On Larkfield’s test period, lift has the lowest recall@10 of the three, 0.059, and only 0.010 and 0.014 for occasional and loyal customers. It favours rare neighbours with 2 or 3 shared customers, which is thin evidence. Look at recall by segment first, then at coverage and novelty, as Module 3 did.

Recap

  • “People also bought” starts with a co-occurrence count: the customers who touched both products.
  • Counts favour bestsellers. Lift = shared × N / (n₁ × n₂) compares the count with chance. Cosine = shared / √(n₁ × n₂) is between counts and lift.
  • In the tiny table, count ranks the gloves first for the tent’s page. Lift (2.0) and cosine (0.707) rank the sleeping bag first.
  • On Larkfield, the cosine neighbours of armchair P10020 are five armchairs; the first, P12928, shares 17 customers. Lift’s first neighbour shares only 3.
  • As home-page methods, cooc gets 0.122 and lift 0.059 on the test period, both below trending (0.156).

A product page knows one product. A returning customer has touched many. Next, we combine the neighbours of everything a customer touched into one list, and we explain each suggestion from the data.

Next: Item-item collaborative filtering