Association Rules and Market Basket
Meet Priya. She runs a small corner grocery, and at the end of each day she has a stack of till receipts. Every receipt is one shopper's basket: a list of what that one person bought. Priya has a hunch that bread and butter keep turning up on the same receipt, and she is tempted to move them onto the same shelf. But she pauses. Bread is on almost every receipt anyway. Maybe bread and butter just look connected because bread is popular, not because the two go together. How can she tell a real buy-this-with-that pattern from a coincidence?
That question, asked over a pile of baskets, is market basket analysis, and the tool below is where this lesson lands. Pick a rule like "if bread, then butter" and read its three numbers. By the end you will know exactly what each one means and which one settles Priya's question.
In Lesson 7 you drew a 2-D map to see which data points group together. Here we drop geometry entirely and ask a different unsupervised question: not "which points cluster?" but "which items get bought together?" No target variable, no labels, just baskets and the patterns hiding in them.
By the end of this lesson you will be able to:
- Read a set of baskets as transactions and name the parts of a rule \(X \Rightarrow Y\)
- Compute support, confidence, and lift by hand, and say what each measures
- Explain why confidence alone misleads, and how lift settles Priya's bread-and-butter question
- Mine and rank rules at scale with the
arulespackage, and read them honestly
Prerequisites: you can run R and read its printed output, and you know what a proportion is ("4 of 10" is 0.4). No earlier lesson in this course is required; this is a fresh question. The course landing lists the rest.
The receipts: transactions and itemsets
Here is Priya's day: ten receipts. Each row is one shopper's basket.
| Receipt | Items |
|---|---|
| 1 | bread, butter, milk |
| 2 | bread, butter |
| 3 | bread, milk |
| 4 | beer, chips |
| 5 | bread, butter, jam |
| 6 | beer, chips, salsa |
| 7 | bread, butter |
| 8 | milk, cereal |
| 9 | beer, chips |
| 10 | bread, jam |
Three words name what we are looking at. A transaction is one basket (one receipt). An item is one thing on it (bread, milk). An itemset is any group of items, like {bread, butter}. And an association rule is written \(X \Rightarrow Y\), read "shoppers who buy the items in \(X\) also buy the items in \(Y\)." The left side \(X\) is the antecedent ("if"), the right side \(Y\) is the consequent ("then"). "If bread, then butter" is the rule \(\{\text{bread}\} \Rightarrow \{\text{butter}\}\).
Since this page runs in its own fresh R session, let us type Priya's receipts in as a list, one basket per entry, and count how often each item shows up.
Bread leads at 6 receipts out of 10. Hold on to that number: it is the reason Priya's hunch needs a second look.