Stop 11 · Unsupervised LearningTopics · 9/10

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.

The mascot places a loaf of bread next to butter and a jar of jam at a small grocery counter.

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

bread butter
0
bread butter
1
bread milk
2
milk
3
bread butter milk
4

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

python
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

~35 min

Quick check

A rule {chips} then {cola} has lift 1.0. What does that mean?

Go deeper

  1. 1Use mlxtend's apriori and association_rules on a groceries dataset.
  2. 2Sort rules by lift and explain the top three in plain words.
  3. 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.

Download
InsideEdge