Association rules: Apriori and FP-growth
Mine frequent itemsets from transactions and turn them into rules with support, confidence and lift.
Animated explanation
The idea
A supermarket wants to know which products are bought together, to place them, bundle them or recommend them. No labels exist: we are looking for patterns in shopping baskets, an unsupervised task.
An itemset is frequent if enough baskets contain it (its support). The trouble is that there are possible itemsets. The Apriori principle saves us: if an itemset is rare, every superset of it is rare too. So we count single items, keep the frequent ones, build pairs only from those, keep the frequent pairs, build triples from them, and so on, pruning huge parts of the search. Frequent itemsets are then split into rules and ranked by confidence and lift.
Measures that matter
Support filters for patterns common enough to act on. Confidence says how reliable the rule is, but a rule pointing to a very popular item (milk) looks confident regardless. Lift corrects for that: lift ; above 1 means a genuine positive association. Leverage and conviction are alternatives.
FP-growth
Apriori rescans the data at every level and generates many candidates. FP-growth compresses the transactions into a prefix tree (FP-tree) with items ordered by frequency, then mines frequent itemsets recursively from conditional trees, with no candidate generation and only two passes over the data. It is the default in most libraries for large datasets.
The algorithm, step by step
- 1Count every single item; keep those with support ≥ min_support ().
- 2For : join frequent -itemsets into -candidates; prune candidates with any infrequent -subset.
- 3Count the surviving candidates in one pass; keep the frequent ones (). Stop when none remain.
- 4For each frequent itemset, generate rules ; keep those above the confidence (and lift) thresholds.
The maths
Support
Confidence
Lift
Strengths and weaknesses
Strengths
- Interpretable if-then patterns
- No labels needed
- Apriori pruning and FP-growth scale to large transaction logs
Weaknesses
- Many redundant or trivial rules; needs thresholds and filtering
- Rare but important associations are missed by support thresholds
- Co-occurrence is not causation
Reach for it when…
Check your understanding
Lambdian Lab
Apriori from scratch
Transactions are Python sets of item names.
- support(itemset, transactions): fraction containing all items (itemset is a frozenset).
- apriori(transactions, min_support): dict {frozenset: support} of all frequent itemsets, built level by level with subset pruning.
- rules(frequent, min_conf): list of (antecedent, consequent, confidence, lift) for every split of every frequent itemset of size ≥ 2 with confidence ≥ min_conf.