๐Ÿ“ˆ Machine Learning ยท Lecture 47 of 47

Association Rule Mining: Apriori, FP-Growth and Market-Basket Analysis

Which items appear together? Association rule mining discovers patterns like "bread and butter imply milk". We define support, confidence and lift, derive the Apriori algorithm, and interpret rules responsibly.

A supermarket notices that customers who buy lentils and rice often buy cooking oil. A pharmacy sees certain medicines prescribed together. A learning platform finds that students who complete the statistics module usually attempt the machine learning module next. Association rule mining finds such co-occurrence patterns in large collections of transactions. It is an unsupervised, highly interpretable technique and one of the classic methods of data mining.

Terminology#

  • A transaction is a set of items (one shopping basket, one patient's prescriptions).
  • An itemset is any set of items, e.g. {rice, lentils}.
  • An association rule $X \Rightarrow Y$ says: transactions containing $X$ tend to contain $Y$ ($X \cap Y = \emptyset$).

Measuring rules#

For $N$ transactions:

$$ \text{support}(X) = \frac{\#\{\text{transactions containing } X\}}{N} $$
$$ \text{confidence}(X \Rightarrow Y) = \frac{\text{support}(X \cup Y)}{\text{support}(X)} = \hat{P}(Y \mid X) $$
$$ \text{lift}(X \Rightarrow Y) = \frac{\text{confidence}(X \Rightarrow Y)}{\text{support}(Y)} = \frac{\hat{P}(X, Y)}{\hat{P}(X)\,\hat{P}(Y)} $$
  • Support โ€” how common the pattern is.
  • Confidence โ€” how often the rule is right when it applies.
  • Lift โ€” how much more likely $Y$ is given $X$ than in general. Lift > 1 indicates positive association; = 1 independence; < 1 negative association.

The mining problem#

Find all rules with support โ‰ฅ min_support and confidence โ‰ฅ min_confidence. The difficulty: with $d$ items there are $2^d$ possible itemsets. For a shop with 10,000 products, brute force is impossible.

The solution splits into two steps:

  1. Find all frequent itemsets (support โ‰ฅ threshold).
  2. Generate high-confidence rules from them.

The Apriori principle#

Every subset of a frequent itemset is frequent. Equivalently: if an itemset is infrequent, all its supersets are infrequent. This anti-monotonicity of support lets us prune the search massively.

Apriori algorithm (Agrawal & Srikant, 1994):

  1. Count single items; keep frequent ones ($L_1$).
  2. For $k = 2, 3, \dots$: generate candidate $k$-itemsets by joining frequent $(k-1)$-itemsets; prune any candidate with an infrequent $(k-1)$-subset; scan the data to count support; keep the frequent ones ($L_k$).
  3. Stop when no new frequent itemsets appear.

Its weakness is repeated database scans and potentially huge candidate sets.

FP-Growth#

FP-Growth (Han et al., 2000) compresses the database into a prefix tree (the FP-tree), where transactions sharing frequent items share paths. It then mines frequent itemsets recursively from conditional trees without candidate generation, needing only two scans of the data. It is usually much faster than Apriori on large datasets.

Example#

python
import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import fpgrowth, association_rules

transactions = [
    ["rice", "lentils", "oil", "salt"],
    ["rice", "lentils", "oil"],
    ["rice", "oil", "sugar"],
    ["lentils", "oil", "onion"],
    ["rice", "lentils", "onion", "oil"],
    ["tea", "sugar", "milk"],
    ["tea", "sugar", "biscuits"],
    ["tea", "milk", "biscuits", "sugar"],
    ["rice", "lentils", "salt"],
    ["tea", "sugar"],
]
te = TransactionEncoder()
df = pd.DataFrame(te.fit_transform(transactions), columns=te.columns_)

itemsets = fpgrowth(df, min_support=0.3, use_colnames=True)
rules = association_rules(itemsets, metric="confidence", min_threshold=0.7)
cols = ["antecedents", "consequents", "support", "confidence", "lift"]
print(rules[cols].sort_values("lift", ascending=False).round(2).to_string(index=False))

Rules such as {rice, lentils} โ‡’ {oil} and {tea} โ‡’ {sugar} emerge with lift well above 1.

Applications#

  • Retail: store layout, cross-selling, promotions, bundle design.
  • Healthcare: co-occurring diagnoses or adverse drug combinations (for further study, not conclusions).
  • Web usage mining: pages visited together; navigation design.
  • Education: modules or resources that students use together.
  • Supply planning: items requested together at distribution points, helping pack kits efficiently.

Interpreting rules responsibly#

  • Association is not causation. Buying a phone case does not cause buying a phone; they co-occur.
  • Multiple testing: mining thousands of rules guarantees some spurious ones. Validate rules on held-out transactions or over time.
  • Thresholds strongly shape the output: too high misses interesting niche patterns; too low floods analysts with trivial rules.
  • Redundancy: many rules restate each other; summarise with closed or maximal itemsets.
JA
Written by

Janin A Apurba

B.Sc. in CSE, AUST ยท Advanced ICT Officer, CNRS-UNHCR. Teaching AI, ML and Deep Learning to the next generation of engineers and researchers.

Keep learning

Related lectures

๐Ÿ“ˆ Machine Learning

Learning Theory: PAC Learning, VC Dimension and Generalisation Bounds

Why should a model that fits training data work on new data? Learning theory answers precisely. We develop PAC learning, finite-class bounds, the VC dimension, and discuss what these bounds do and do not explain about deep learning.

Advancedโฑ 6 min#095
๐Ÿ“ˆ Machine Learning

Active Learning: Letting the Model Choose What to Label

If labelling is expensive, label the most informative examples first. We cover pool-based active learning, uncertainty and diversity sampling, query-by-committee, and the practical pitfalls of real annotation loops.

Intermediateโฑ 5 min#094
๐Ÿ“ˆ Machine Learning

Semi-Supervised Learning: Learning from Few Labels and Many Unlabelled Examples

Labels are expensive; unlabelled data is cheap. We study the assumptions that make unlabelled data useful and the main techniques โ€” self-training, label propagation, consistency regularisation and FixMatch.

Intermediateโฑ 5 min#093