Stop 08 · Unsupervised LearningTopics · 6/7

Association Rule Mining (Market Basket Analysis)

Part of the Data Science 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.

Finding patterns like 'customers who buy X also buy Y' from transaction data — the classic technique behind retail recommendation logic like the Apriori algorithm.

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. 1Run a basic Apriori analysis on a sample transaction dataset.
  2. 2Learn the terms support, confidence, and lift, and what each measures.
  3. 3Identify a real 'frequently bought together' pattern you'd expect in a retail dataset.

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