Foundations to Frontier
Tests
D. Classical Machine Learning › D12 Association Rule Mining

Apriori

Association rule mining looks for items that appear together in transactions (products in shopping baskets, alarms in maintenance logs, symptoms in reports) and turns frequent combinations into rules such as 'customers who buy diapers also buy beer'. Apriori (Agrawal and Srikant, 1994) finds every itemset whose support, the fraction of transactions containing it, reaches a threshold. It does this level by level, generating candidate (k + 1)-itemsets only from frequent k-itemsets and pruning any candidate with an infrequent subset, which is the downward-closure or Apriori property. Rules are then read off the frequent itemsets and filtered by confidence. The algorithm is simple and exact, but it scans the data once per level and its candidate sets explode at low support, and its standard output is flooded with high-confidence rules that merely restate popular items. This page covers support, confidence, lift, leverage and conviction, candidate generation and pruning, complexity, and how to filter rules sensibly. In the exercises you implement Apriori and rule generation and check them against brute force; then you measure candidate explosion and the confidence trap on synthetic baskets.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Things that go together

A supermarket wants to know which products are bought together, so it can place them nearby or bundle them. Looking through thousands of receipts, you might notice that coffee, milk and sugar often appear on the same receipt. Apriori finds all such groups systematically. It uses a simple observation: if a pair of items is rare, any larger group containing that pair must be at least as rare, so there is no need to count it.

Apriori: frequent itemsets, level by level, and the rules they give
size 1: 12/12size 2: 66/21size 3: 16/6size 4: 1/0
39 frequent itemsets · 25 rules · 14 of them with lift < 1.2
coffee + sugar → milk · sup 0.08 · conf 0.80 · lift 2.21
milk + sugar → coffee · sup 0.08 · conf 0.73 · lift 3.19
cheese → bread · sup 0.12 · conf 0.73 · lift 1.23
bread + salsa → chips · sup 0.06 · conf 0.69 · lift 2.74
milk + coffee → sugar · sup 0.08 · conf 0.66 · lift 3.03
eggs → bread · sup 0.14 · conf 0.66 · lift 1.11
salsa → chips · sup 0.11 · conf 0.65 · lift 2.61
milk + coffee → bread · sup 0.08 · conf 0.64 · lift 1.08
coffee + sugar → bread · sup 0.07 · conf 0.63 · lift 1.07
milk → bread · sup 0.23 · conf 0.62 · lift 1.04
chips → bread · sup 0.15 · conf 0.61 · lift 1.03
milk + chips → bread · sup 0.06 · conf 0.60 · lift 1.01
coffee → bread · sup 0.14 · conf 0.60 · lift 1.00
sugar → bread · sup 0.13 · conf 0.60 · lift 1.00
Bars: candidates counted (light) and frequent itemsets (dark) for each itemset size; Apriori never counts a candidate that has an infrequent subset. Lower the minimum support and both grow quickly. In the rule list, rules ending in "bread" often have high confidence simply because bread is in 60% of baskets: their lift is about 1 (greyed), meaning no association. The planted patterns show lifts of about 2–4. At low support, chance alone can also give lifts of 1.3–1.5 (try new baskets), so a lift needs a significance check before it is believed.
Why engineers care

Maintenance and alarm logs are transactions too: which fault codes, warnings and part replacements occur together on the same flight or work order? Frequent co-occurrence patterns point to common causes, cascading failures or documentation habits. The same tools mine software crash reports, test failures and configuration combinations.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Support of an itemset is…
  2. 2. If {coffee, milk} is rare, then {coffee, milk, sugar} is…
  3. 3. Apriori finds itemsets…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Support, confidence and lift by hand

Take five baskets: (bread, milk), (bread, diapers, beer, eggs), (milk, diapers, beer, cola), (bread, milk, diapers, beer) and (bread, milk, diapers, cola).

Support. support(X) = fraction of baskets containing X:

  • support(diapers) = 4/5 = 0.8;
  • support(beer) = 0.6;
  • support(beer, diapers) = 0.6.

Confidence. confidence(A → B) = support(A ∪ B)/support(A), an estimate of P(B | A):

  • beer → diapers: 0.6/0.6 = 1;
  • diapers → beer: 0.6/0.8 = 0.75.

Lift. lift(A → B) = confidence/support(B) = P(A, B)/(P(A)P(B)). For beer → diapers, 1/0.8 = 1.25. Lift is symmetric in A and B; 1 means independence.

Other measures.

  • Leverage: P(A, B) − P(A)P(B) = 0.6 − 0.48 = 0.12.
  • Conviction: (1 − P(B))/(1 − conf). For diapers → beer, (1 − 0.6)/(1 − 0.75) = 1.6; it is infinite for a rule that is never violated.

The steps in plain English (Apriori):

  1. Count every single item; keep those with support ≥ s.
  2. Join frequent k-itemsets that share k − 1 items into (k + 1)-candidates.
  3. Prune candidates with any infrequent k-subset, which cannot be frequent.
  4. Count the survivors in one scan of the data; keep the frequent ones; repeat until no candidates remain.
  5. For each frequent itemset, emit the rules A → (itemset − A) with confidence ≥ c, then rank by lift or another interest measure.
📝 Quick check (Level 1)
0/3 answered
  1. 1. confidence(A → B) estimates…
  2. 2. A lift of 1 means…
  3. 3. Apriori prunes a candidate when…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

Cost, and the confidence trap

Downward closure. If X ⊆ Y then support(Y) ≤ support(X), because every basket containing Y also contains X. So all subsets of a frequent itemset are frequent, and no superset of an infrequent itemset can be frequent. That justifies Apriori’s pruning.

Cost. With d items there are 2^d − 1 possible itemsets, 1.1 × 10¹⁵ for d = 50. Apriori counts only candidates whose subsets are all frequent, but it needs one full data scan per level, and the candidate counts grow rapidly as the threshold falls. Long frequent patterns are expensive: a frequent itemset of size m has 2^m − 1 frequent subsets, all of which must be found.

The confidence trap. Confidence ignores how common B is anyway. If bread appears in 60% of baskets, then any A → bread rule has confidence about 0.6 even when A and bread are unrelated. Such rules pass a confidence threshold and fill the output with noise. Lift, leverage or conviction measure the gain over the base rate.

Exercise 1 (the five baskets, minimum support 0.6):

  • 8 frequent itemsets, identical to a brute-force search over all 63 itemsets;
  • only 13 candidates counted;
  • beer → diapers: support 0.6, confidence 1.00, lift 1.25;
  • 8 rules with confidence ≥ 0.7.
from mlxtend.frequent_patterns import apriori, association_rules
freq = apriori(onehot_df, min_support=0.02, use_colnames=True)
rules = association_rules(freq, metric="lift", min_threshold=1.2)
🐍 Exercise 1: Apriori from scratch
Implement support counting, level-wise candidate generation with Apriori pruning, and rule generation with confidence and lift; verify the frequent itemsets against brute force on the classic five baskets.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Apriori scans the data…
  2. 2. A rule A → staple has high confidence mostly because…
  3. 3. How many frequent subsets does a frequent itemset of size 10 imply?

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Correctness. By downward closure, every frequent (k + 1)-itemset is a join of two frequent k-itemsets sharing k − 1 items, and survives pruning. So Apriori finds all frequent itemsets and nothing else.

Complexity. Counting dominates. With hash trees or tries, each scan checks each transaction against the candidates it can contain. Worst-case output size is exponential, and enumerating frequent itemsets is #P-hard in general. Practical cost depends on the data’s density and the threshold.

Interestingness and statistics.

  • Lift is unstable for rare items.
  • Leverage and conviction behave differently near independence.
  • Fisher’s exact or χ² tests check whether a co-occurrence exceeds chance.
  • With thousands of candidate rules, multiple-testing corrections (Bonferroni, false discovery rate) or holdout validation (Webb, 2007) are essential to avoid “discovering” noise.

Condensed representations. Closed itemsets (no superset with the same support) and maximal itemsets (no frequent superset) summarise the frequent set without losing support information, in the closed case, or keep only its border, in the maximal case.

Interview questions.

  • Why do association rules often look obvious or useless? Confidence rewards popular consequents, and low thresholds generate thousands of redundant rules. Rank by lift or leverage, test significance, and remove rules implied by simpler ones.
  • Apriori or FP-Growth? FP-Growth for large or dense data, since it avoids candidate generation and makes only two scans. Apriori is simpler and fine for sparse data with high thresholds.
📝 Quick check (Level 3)
0/3 answered
  1. 1. A closed itemset has…
  2. 2. With thousands of candidate rules, significance needs…
  3. 3. Lift is unstable for…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Candidate explosion and noisy rules

History.

  • R. Agrawal, T. Imieliński and A. Swami (1993) introduced association rule mining.
  • R. Agrawal and R. Srikant (1994) introduced Apriori.
  • J. Han, J. Pei and Y. Yin (2000) introduced FP-Growth.
  • M. Zaki (2000) introduced Eclat.
  • G. Webb (2007) introduced statistically sound rule discovery.

Exercise 2. 2000 synthetic baskets over 50 items with Zipf-like popularity. Item 0 is a staple in 60% of baskets, and three planted patterns, items (1, 7, 12), (20, 25) and (30, 33, 38), each appear together in 8% of baskets.

minimum support candidates counted frequent itemsets largest frequent size
0.10 156 22 2
0.05 536 75 4
0.02 1478 217 5
0.01 2485 585 5

At support ≥ 0.02 and confidence ≥ 0.5 there were 170 rules:

  • 78 had lift below 1.2;

  • 79 predicted the staple item, with a mean lift of just 1.03;

  • all 3 planted patterns were found, with a mean lift of 5.54.

  • Lowering the threshold tenfold multiplied the candidate count by 16 and the frequent itemsets by 27, and every level needs another full data scan.

  • Nearly half the confident rules were the confidence trap: “X → staple” with lift about 1, carrying no information beyond the staple’s popularity.

  • Ranking by lift separated the planted associations cleanly (5.5 against 1.0). A lift or leverage filter, plus a significance test, is the minimum for usable output.

🐍 Exercise 2 (research): candidate explosion and the confidence trap
On 2000 synthetic baskets with a staple item and three planted patterns, count Apriori's candidates and frequent itemsets at supports 0.10–0.01; then generate rules at support 0.02 and confidence 0.5 and count those with low lift, those predicting the staple, and the planted patterns found.

Association rules in AI, robotics and aerospace

  • Maintenance logs: co-occurring fault codes and part replacements reveal cascades and common root causes, which become candidates for reliability engineering.
  • Alarm flood analysis: frequent alarm combinations in control rooms and cockpits identify redundant or consequential alarms to suppress or group.
  • Test and configuration mining: combinations of software modules or settings that co-occur with failures in large regression suites.

References

  1. R. Agrawal, R. Srikant (1994). “Fast algorithms for mining association rules.” Proc. VLDB.
  2. R. Agrawal, T. Imieliński, A. Swami (1993). “Mining association rules between sets of items in large databases.” Proc. ACM SIGMOD.
  3. P.-N. Tan, V. Kumar, J. Srivastava (2002). “Selecting the right interestingness measure for association patterns.” Proc. ACM KDD.
  4. G. I. Webb (2007). “Discovering significant patterns.” Machine Learning 68(1).