📖 Market basket analysis finds which items are bought together. Association rule: A → B (people who buy A also buy B). Apriori is the classic algorithm to find them.
Apriori property: every subset of a frequent itemset must also be frequent. So if {bread, beer} is not frequent, no bigger set containing it can be frequent. This lets us prune candidates early.
Worked example (minimum support count = 3 out of 5)
| Transaction | Items |
|---|
| T1 | bread, milk |
| T2 | beer, bread, diaper, eggs |
| T3 | beer, cola, diaper, milk |
| T4 | beer, bread, diaper, milk |
| T5 | bread, cola, diaper, milk |
| Item (C1) | Count | Frequent (L1)? |
|---|
| beer | 3 | Yes |
| bread | 4 | Yes |
| cola | 2 | No (removed) |
| diaper | 4 | Yes |
| eggs | 1 | No (removed) |
| milk | 4 | Yes |
| 2-itemset (C2) | Count | Frequent (L2)? |
|---|
| {beer, bread} | 2 | No |
| {beer, diaper} | 3 | Yes |
| {beer, milk} | 2 | No |
| {bread, diaper} | 3 | Yes |
| {bread, milk} | 3 | Yes |
| {diaper, milk} | 3 | Yes |
| 3-itemset (C3) | Count | Frequent? |
|---|
| {beer, bread, diaper} | 2 | No |
| {beer, bread, milk} | 1 | No |
| {beer, diaper, milk} | 2 | No |
| {bread, diaper, milk} | 2 | No |
Result: Frequent itemsets are the 4 single items and 4 pairs: {bread, milk}, {bread, diaper}, {milk, diaper}, {diaper, beer}. No 3-item set reaches count 3, so the algorithm stops.
| Rule | Support | Confidence | Lift |
|---|
| beer → diaper | 60% | 100% | 1.25 |
| diaper → beer | 60% | 75% | 1.25 |
| bread → diaper | 60% | 75% | 0.94 |
| diaper → bread | 60% | 75% | 0.94 |
| bread → milk | 60% | 75% | 0.94 |
| milk → bread | 60% | 75% | 0.94 |
| diaper → milk | 60% | 75% | 0.94 |
| milk → diaper | 60% | 75% | 0.94 |
💡 Reading a rule: beer → diaper has support 60%, confidence 100% and lift 1.25. Every beer buyer also bought diapers, and this is 1.25 times more often than by chance. A shop can keep these items close or offer a combo.
Uses of market basket analysis: store layout, combo offers and cross-selling, recommendations, stock planning.