Association rules and Apriori
Association rules (market-basket style) find item-item dependencies in transaction data; Apriori is the classic algorithm that generates frequent itemsets by repeated candidate generation and pruning using the Apriori property.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Association rules are used by engineers working on Business Intelligence for tasks such as market-basket analysis, cross-sell and up-sell recommendations, store layout decisions, and feature engineering for predictive models. Apriori is the standard algorithm taught to find frequent co-occurring items from transactional logs so rules of the form “if A and B, then C” can be discovered.
Key ideas
Start with definitions (each bolded when first used):
- Transaction: a single record of items bought together (e.g. one customer's shopping basket). Each transaction is a set of items.
- Item: a distinct product or attribute that may appear in transactions.
- Itemset: a set of one or more items (also called k-itemset when it has k items).
- Support count (notation:
σ(X)): the number of transactions that contain itemset X. - Support (
s(X)): the fraction of transactions that contain X; s(X) = σ(X) / N, where N is total number of transactions. - Frequent itemset: an itemset whose support >= minimum support threshold (
minsup). - Closed frequent itemset: a frequent itemset with no proper superset having the same support.
- Maximal frequent itemset: a frequent itemset for which no proper superset is frequent.
- Association rule: an implication of the form
X -> Ywhere X and Y are disjoint itemsets; the rule means “when X appears, Y tends to appear”. - Confidence (
conf(X -> Y)): conditional probability that transactions containing X also contain Y, conf(X -> Y) = s(X ∪ Y) / s(X). - Lift (
lift(X -> Y)): measures dependence, lift = conf(X -> Y) / s(Y). A lift > 1 means positive association, =1 means independence, <1 negative association. - Apriori property: "All non-empty subsets of a frequent itemset must also be frequent." This is fundamental for pruning candidates.
Proof of Apriori property (short, exact): Assume itemset L is frequent: s(L) ≥ minsup. Let S be any non-empty subset of L. Every transaction that contains L also contains S, so σ(L) ≤ σ(S), hence s(L) ≤ s(S). Since s(L) ≥ minsup, we get s(S) ≥ minsup. Therefore s(S) ≥ minsup, so S is frequent. Property proven.
Apriori algorithm idea (step-by-step):
- Count support for all 1-itemsets, keep those with s ≥ minsup as L1.
- For k = 2, 3, ...
- Join step: generate candidate k-itemsets Ck by joining pairs of frequent (k−1)-itemsets (Lk-1) that share first k−2 items (lexicographic join). Example rule: join {a,b,c} and {a,b,d} -> candidate {a,b,c,d}.
- Prune step: remove any candidate in Ck that has a (k−1)-subset not in Lk-1 (using Apriori property). This avoids counting candidates that cannot be frequent.
- Scan the transaction database to get support counts for remaining candidates, form Lk as those with s ≥ minsup.
- Repeat until no new frequent itemsets are found.
- Generate association rules from each frequent itemset f: for each non-empty subset s ⊂ f, form rule s -> (f\s). Compute confidence = s(f)/s(s); keep rules with conf ≥ minconf.
Assumptions and limitations to state explicitly:
- Transactions are sets (no order, no duplicates within a transaction).
- Database fits disk access model: Apriori requires multiple passes over the DB (one per k) and a lot of candidate storage.
- Choice of minsup and minconf strongly affects output size and running time.
What to draw for an exam diagram:
- Draw a lattice of itemsets (powerset) for 3 items {A,B,C} as 4 levels: top level empty set, then {A},{B},{C}, then {A,B},{A,C},{B,C}, then {A,B,C}. Mark which node(s) are frequent (shade them). Then show join: connect {A,B} and {A,C} joining into {A,B,C} and show prune if any subset is infrequent.
Formulas
s(X) = σ(X) / N — s(X) is support of itemset X; σ(X) is support count (number of transactions containing X); N is total number of transactions (unit: dimensionless fraction). Applies whenever defining support.
conf(X -> Y) = s(X ∪ Y) / s(X) — confidence of rule X -> Y; s(...) are supports as above (dimensionless). Applies when testing rule strength.
supp(X -> Y) = s(X ∪ Y) — support of a rule X -> Y equals the support of the union itemset (dimensionless).
lift(X -> Y) = conf(X -> Y) / s(Y) = s(X ∪ Y) / (s(X)·s(Y)) — lift measures statistical dependence between X and Y (dimensionless). Applies when comparing to independence baseline.
σ(X ∪ Y) ≤ σ(X) — support count of a superset is at most support count of subset. This inequality underlies Apriori pruning.
(minsup, minconf) — notation to remember: minimum support threshold and minimum confidence threshold used by Apriori.
Worked examples
Example 1 — computing support, confidence, lift (numerical): Dataset (N = 5 transactions): T1 = {Milk, Bread, Butter} T2 = {Bread, Diaper, Beer, Eggs} T3 = {Milk, Bread, Diaper, Beer} T4 = {Bread, Milk, Diaper, Beer} T5 = {Milk, Bread, Butter}
Step 1: compute σ and s for single items (counts):
- σ(Bread) = 5 → s(Bread) = 5/5 = 1.0
- σ(Milk) = 4 → s(Milk) = 4/5 = 0.8
- σ(Diaper) = 3 → s(Diaper) = 3/5 = 0.6
- σ(Beer) = 3 → s(Beer) = 3/5 = 0.6
- σ(Butter) = 2 → s(Butter) = 2/5 = 0.4
- σ(Eggs) = 1 → s(Eggs) = 1/5 = 0.2
Let minsup = 0.4 (40%), so frequent 1-itemsets: Bread, Milk, Diaper, Beer, Butter (Eggs dropped).
Step 2: compute pair supports (σ and s) by scanning transactions (counts given):
- σ(Milk,Bread)=4 → s=0.8
- σ(Milk,Butter)=2 → s=0.4
- σ(Milk,Diaper)=2 → s=0.4
- σ(Milk,Beer)=2 → s=0.4
- σ(Bread,Butter)=2 → s=0.4
- σ(Bread,Diaper)=3 → s=0.6
- σ(Bread,Beer)=3 → s=0.6
- σ(Diaper,Beer)=3 → s=0.6 (minsup 0.4 keeps all above)
Step 3: form candidate 3-itemsets and count (example):
- {Milk,Bread,Butter}: σ=2 → s=0.4 (frequent)
- {Milk,Bread,Diaper}: σ=2 → s=0.4 (frequent)
- {Milk,Bread,Beer}: σ=2 → s=0.4 (frequent)
- {Milk,Diaper,Beer}: σ=2 → s=0.4 (frequent)
- {Bread,Diaper,Beer}: σ=3 → s=0.6 (frequent)
Step 4: example association-rule generation from frequent 4-itemset
- Frequent 4-itemset f = {Milk,Bread,Diaper,Beer} has σ=2 → s(f)=0.4.
- Consider rule {Milk,Diaper,Beer} -> Bread. σ({Milk,Diaper,Beer}) = 2 so conf = s(f)/s({Milk,Diaper,Beer}) = (2/5)/(2/5) = 1.0. With minconf = 0.7 this rule is strong. lift = conf / s(Bread) = 1.0 / 1.0 = 1.0.
- Consider rule {Bread,Diaper,Beer} -> Milk. σ({Bread,Diaper,Beer}) = 3 so conf = (2/5)/(3/5) = 2/3 ≈ 0.6667 < 0.7 so this rule fails the confidence threshold.
Final strong rule example (full numeric): Rule: {Milk,Diaper,Beer} -> Bread Formula: conf = s({Milk,Diaper,Beer,Bread}) / s({Milk,Diaper,Beer}) = (2/5) / (2/5) = 1.0 → conf = 1.0 Lift = conf / s(Bread) = 1.0 / 1.0 = 1.0
Example 2 — Apriori trace (short algorithm trace): Use same small dataset above; show one pass:
- L1 = {Bread(5), Milk(4), Diaper(3), Beer(3), Butter(2)} with counts in parentheses.
- Generate C2 by joining L1 pairs; compute supports by one DB pass to get C2 counts listed in Example 1 and prune those below minsup. L2 = pairs listed earlier.
- Join L2 entries to get C3, prune those with any 2-subset not in L2, count supports to get L3 as in Example 1.
- Repeat to get L4 = {Milk,Bread,Diaper,Beer} with σ=2. This trace shows how repeated DB scans and join/prune reduce candidates.
Common mistakes
- Forgetting to prune candidate k-itemsets whose (k−1)-subsets are not all in Lk-1.
- Computing confidence as σ(X)/σ(Y) or dividing in the wrong order. Formula is conf(X->Y) = σ(X∪Y)/σ(X).
- Not checking that antecedent and consequent are disjoint in a rule (X and Y must not overlap).
- Using too-low minsup or minconf without noting that output (rules) can explode exponentially.
For your exam
Types of questions you will see:
- Definitions and short notes: define support, confidence, lift, Apriori property, candidate generation and pruning.
- Derivations / proofs: prove Apriori property; show why σ(superset) ≤ σ(subset).
- Algorithm trace: run Apriori on a small transaction set (student must show L1, C2, L2, C3, L3, etc.). Include counts and pruning steps.
- Numerical rule quality: compute support, confidence and lift for given rule(s) and decide if rule passes thresholds.
- Counting rules: from a frequent k-itemset, the number of possible non-empty rules is 2^k - 2 (all non-empty proper subsets as antecedents).
- Discussion/limitations: explain complexity, number of DB passes, and alternatives (mention FP-growth by name).
What a full-marks answer includes:
- Clear definitions with symbols (σ, s, minsup, minconf), a correct statement and short proof of Apriori property, a correct step-by-step run of Apriori with candidate generation and explicit pruning checks, correct numeric computations with N shown, and final rules with support/confidence/lift and decision against thresholds.
Quick check
- State the Apriori property in one sentence.
- If σ({A,B,C}) = 3 and N = 10, what is s({A,B,C}) and what is conf({A,B} -> {C}) if σ({A,B}) = 4?
- What does lift = 1 signify about two itemsets?
Answers: 1) All non-empty subsets of a frequent itemset are also frequent. 2) s = 3/10 = 0.3; conf = s({A,B,C})/s({A,B}) = (3/10)/(4/10) = 3/4 = 0.75. 3) Lift = 1 means the antecedent and consequent are statistically independent (no association).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?