Association Rules (Apriori)
Part of the Machine Learning Roadmap
In short
People who buy bread often buy butter too. Association rules find these 'bought together' patterns across thousands of bills, which drives shelf layouts, combo offers and 'frequently bought together' lists.
Association rule mining finds items that often appear together, like bread and butter in grocery baskets. Support, confidence and lift measure how common and how meaningful each rule is.

Think of it like this
The kirana owner's memory
Your local kirana uncle keeps candles next to matchboxes and butter near the bread. No one taught him statistics. Years of watching bills told him which things leave the shop together. Association rule mining does the same with millions of receipts.
Watch it happen
Five bills, one rule: bread then butter
1/4Each cell is one customer's basket. We test the rule {bread} then {butter}.
The big ideas
What you need to know
Support: how common
The share of all baskets containing the itemset. Low support rules may be flukes.
Confidence: how reliable
Of baskets with A, the share that also have B. It's P(B given A).
Lift: better than chance?
Lift above 1 means a real link. Lift near 1 means B is just popular anyway.
Apriori prunes
If an itemset is rare, every bigger set containing it is rare too. Apriori skips those to save time.
See it in code
Walk through the code
1import pandas as pd2from mlxtend.frequent_patterns import apriori, association_rules3 4baskets = pd.DataFrame(5 [[1, 1, 0], [1, 1, 0], [1, 0, 1], [0, 0, 1], [1, 1, 1]],6 columns=["bread", "butter", "milk"]).astype(bool)7 8itemsets = apriori(baskets, min_support=0.4, use_colnames=True)9rules = association_rules(itemsets, metric="confidence", min_threshold=0.6)10print(rules[["antecedents", "consequents", "support", "confidence", "lift"]])1/3One row per bill, one True/False column per product.
The same five bills as the scene, one-hot encoded. Install the library with pip install mlxtend.
Save yourself the pain
Common mistakes
Oops
Ranking rules by confidence alone.
Do this instead
Check lift too. 'Anything then milk' can have high confidence just because everyone buys milk.
Oops
Reading a rule as cause and effect.
Do this instead
Rules show items bought together, not that one causes the other. Test offers with an experiment.
Your mission
Mine a grocery dataset
Quick check
A rule {chips} then {cola} has lift 1.0. What does that mean?
Go deeper
- 1Use mlxtend's apriori and association_rules on a groceries dataset.
- 2Sort rules by lift and explain the top three in plain words.
- 3Suggest one store layout or combo offer based on a rule.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.