Skip to content
Lambdian

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 2d2^d 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 X⇒YX \Rightarrow Y and ranked by confidence and lift.

Measures that matter

Support filters for patterns common enough to act on. Confidence P(Y∣X)P(Y\mid X) says how reliable the rule is, but a rule pointing to a very popular item (milk) looks confident regardless. Lift corrects for that: lift =P(Y∣X)/P(Y)= P(Y\mid X)/P(Y); 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

  1. 1
    Count every single item; keep those with support ≥ min_support (L1L_1).
  2. 2
    For k=2,3,…k = 2, 3, \dots: join frequent (k−1)(k-1)-itemsets into kk-candidates; prune candidates with any infrequent (k−1)(k-1)-subset.
  3. 3
    Count the surviving candidates in one pass; keep the frequent ones (LkL_k). Stop when none remain.
  4. 4
    For each frequent itemset, generate rules X⇒YX\Rightarrow Y; keep those above the confidence (and lift) thresholds.

The maths

Support

supp⁡(X)=P(X)\operatorname{supp}(X) = P(X)

Confidence

conf⁡(X⇒Y)=P(Y∣X)=supp⁡(X∪Y)supp⁡(X)\operatorname{conf}(X\Rightarrow Y) = P(Y\mid X) = \frac{\operatorname{supp}(X\cup Y)}{\operatorname{supp}(X)}

Lift

lift⁡(X⇒Y)=P(X∪Y)P(X)P(Y)\operatorname{lift}(X\Rightarrow Y) = \frac{P(X\cup Y)}{P(X)P(Y)}

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…

Transaction or event data: market baskets, web clickstreams, co-prescribed drugs, frequently co-occurring alarms.

Check your understanding

Question 1

Q1{bread, butter} is infrequent. What can Apriori conclude about {bread, butter, jam}?

Question 2

Q2Rule {chips} → {milk} has confidence 0.8, and 80% of all baskets contain milk. Its lift is…

Question 3

Q3What is FP-growth's main advantage over Apriori?

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.
Open the labPython + NumPy runs in your browser. Sign-in keeps your code and attempts.