There is a retail legend: mine a supermarket's receipts and you find that shoppers who buy diapers disproportionately also buy beer (tired new parents, the story goes). True or not, it captures the goal of association rule mining: discover, with no labels and no target, which items travel together, and how strongly.
Mine baskets for "A goes with B" rules: judge each by support (common enough?), confidence (reliable?), and above all lift (stronger than chance?), and use Apriori's pruning trick, or the faster FP-Growth, to find them without checking every combination.
Market Baskets and Rules
The input is a pile of transactions, each a set of items bought together. The output is a set of rules of the form antecedent → consequent ("if a basket has chips and soda, it probably has salsa"). This is unsupervised: nothing is being predicted or labeled, we are simply describing structure, which items keep company.
Before mining, receipts (stored as one row per item) are reshaped into a basket matrix: one row per trip, one True/False column per item. Our grocery data becomes a 900-trip by 25-item grid of booleans, the shape every mining algorithm expects.
Support, Confidence & Lift
Three metrics turn a co-occurrence into an actionable rule, and getting the difference between them right is the whole game.
Support is popularity, the fraction of baskets holding the whole itemset. It keeps us honest (rare combos are statistically flimsy) and, as we will see, powers the search. Confidence is reliability, given A, how often does B follow? But confidence has a trap: if B is popular, almost any rule "… → B" scores high. Lift fixes that by dividing confidence by B's baseline rate. Lift > 1 is a genuine positive association, = 1 is independence, < 1 means the items repel.
Finding Rules: Apriori & FP-Growth
With even a few dozen items there are astronomically many possible itemsets, checking every one is hopeless. Apriori escapes the explosion with one elegant observation, the downward-closure principle: if an itemset is infrequent, every larger set containing it must be infrequent too. So the moment a set falls below minimum support, Apriori prunes it and never looks at any of its supersets.
Because it explores only the survivors, Apriori scans a tiny corner of the space and still finds every frequent itemset, here, 61 of them above 3% support. FP-Growth reaches the same answer by a different route: it compresses the transactions into a tree and reads patterns off it without repeated scans, so it runs much faster on large catalogs. Same rules, better speed, which is why FP-Growth is the workhorse on real retail data.
Real-World Example: Market-Basket Analysis
The original and still biggest use of association rules is market-basket analysis: what to bundle, what to put on adjacent shelves, what to recommend at checkout.
900 grocery transactions in long format, one row per item per
transaction_id (25 distinct items). Pivot to a one-hot basket matrix, then mine.
Ranking the mined rules by lift surfaces the genuine patterns, and rejects the impostors:
| Rule | Confidence | Lift | Verdict |
|---|---|---|---|
| {chips, soda} → salsa | 0.86 | 5.70 | strong, bundle for game day |
| diapers → beer | 0.74 | 5.05 | the legend, reproduced |
| butter → jam | 0.80 | 4.31 | real, classic pairing |
| bananas → bread | 0.37 | 1.10 | coincidence, bread is just popular |
The last row is the lesson. Its 37% confidence looks like something, until you remember bread sits in 33% of all baskets anyway. Buying bananas barely moves the needle, so its lift is ~1: no real link. Rank by lift, filter by support, then act. That is market-basket analysis in one sentence.
Association Rules in Machine Learning & AI
The "what goes with what" question reaches far beyond the grocery aisle.
| Idea | Where it is used |
|---|---|
| Market-basket rules | cross-sell, product bundling, store layout, checkout suggestions |
| Lift | separating real association from popularity in any co-occurrence data |
| Apriori / FP-Growth | web clickstream, medical co-morbidities, fraud pattern discovery |
| Item co-occurrence | the classic, transparent core of "customers also bought" recommenders |
| Frequent-pattern mining | text and log mining, bioinformatics motif discovery |
Association rules are the original recommender system, and their great virtue is being transparent: a rule is a plain, auditable "if this then that," not a black box. Modern large-scale recommenders have largely moved to matrix factorization and neural embeddings (dimensionality reduction from the Dimensionality Reduction chapter, applied to users and items), which capture subtler taste than raw co-occurrence. But the association-rule mindset endures: lift remains the standard way to ask whether two events are linked or merely both common, a question that surfaces everywhere from A/B analysis to fraud detection to feature discovery. Simple, explainable, and still surprisingly hard to beat for interpretable recommendations.
Mine baskets in Python
The companion notebook reshapes receipts into a basket matrix, mines frequent itemsets with Apriori, ranks rules by support, confidence, and lift, exposes the popular-consequent trap, and confirms FP-Growth gives identical results faster, each cell explained.
View opens the rendered notebook instantly. Open in Colab runs it live. To run
locally, install numpy, pandas, mlxtend, matplotlib, and
openpyxl.
🎓 Key Takeaways
- ✓Association rules (A → B) describe which items co-occur, unsupervised, no target.
- ✓Support = how common the itemset is; it keeps rules trustworthy and powers the search.
- ✓Confidence = P(B given A), but it is inflated when B is popular.
- ✓Lift = confidence vs B's baseline; > 1 is a real association. Rank by lift.
- ✓Apriori prunes with downward closure; FP-Growth gets the same rules faster.
Practice Challenges
Five short challenges. Try them with mlxtend before checking the solutions.
Most frequent item
Build the basket matrix and report the item with the highest support.
basket.mean().Frequent itemsets
Run Apriori at min_support = 0.05 and count the frequent itemsets.
apriori(basket, min_support=0.05, use_colnames=True).Top rule by lift
Mine rules and report the single highest-lift rule.
association_rules(freq, metric='lift'); sort by lift.Confidence vs lift
Find the lift of bananas → bread and explain why its confidence misleads.
FP-Growth agreement
Confirm FP-Growth returns the same frequent itemsets as Apriori.
frozenset itemsets.A fully-worked solutions notebook walks through all five challenges, each verified in code. Try them yourself first, then compare.
Quiz: Test Yourself
Eight quick questions on association rules. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved, so you can hop back to the chapter and return anytime.
We have grouped, compressed, and linked. The last unsupervised task is spotting what does not fit. Anomaly Detection finds the rare, suspicious points, fraud, faults, and intrusions, with statistical, distance-, and density-based methods, and the isolation forest.