Association Rules
Discover items that often appear together. Learn how market basket analysis finds patterns such as “customers who buy bread may also buy butter” using support, confidence, and lift.
The big idea
Find useful “if this, then that” patterns in data.
Association Rule Learning looks for relationships between items or events that happen together. A common example is market basket analysis, where a shop studies which products customers buy in the same visit.
The method finds patterns in transaction data. It does not prove that buying one item causes someone to buy another.
At a glance
Four useful ideas to remember.
A group of one or more items, such as {bread, milk}.
A pattern written as an IF part and a THEN part.
How often the items appear together in all transactions.
How often the THEN item appears with the IF item, and how much stronger that link is than chance baseline.
1. How do Association Rules work?
The algorithm counts item combinations, then turns useful combinations into rules.
A simple workflow
Each transaction is a basket or event containing one or more items.
Count how many transactions contain each item combination. Keep combinations that meet minimum support.
Split a frequent itemset into an IF part (antecedent) and a THEN part (consequent).
Use confidence and lift to decide which rules are worth studying.
Imagine a grocery shop
The shop checks 100 baskets. If 30 baskets contain both bread and butter, that pair has 30% support.
2. Live Association Rules playground
Change the minimum support and confidence to see which shopping rules remain.
Sample shopping transactions
Each row is one customer's basket.
The sample data is small so you can check the counts by hand. Choose another dataset to explore different buying patterns.
Rules that pass your settings
Sorted by lift, highest first.
| Rule | Support | Confidence | Lift |
|---|---|---|---|
| Press “Find rules” to begin. | |||
How common must an item combination be?
How often should the THEN item appear when the IF items appear?
3. Three key measures
These measures help us understand whether a rule is common and useful.
Support
The share of all transactions that contain both A and B.
Confidence
Of the transactions that contain A, the share that also contain B.
Lift
Compares the rule's confidence with how common B is overall.
How to read lift
A and B appear together about as often as expected if unrelated.
A and B appear together more often than the baseline suggests.
A and B appear together less often than the baseline suggests.
4. Worked example: bread and butter
Use a tiny set of baskets to calculate the three measures.
Ten shopping baskets
| Basket | Items |
|---|---|
| 1 | Bread, Butter, Milk |
| 2 | Bread, Butter |
| 3 | Bread, Milk |
| 4 | Bread, Butter |
| 5 | Butter, Milk |
| 6 | Bread, Butter, Milk |
| 7 | Bread |
| 8 | Butter |
| 9 | Bread, Butter |
| 10 | Milk |
Rule: Bread → Butter
Five baskets contain both bread and butter.
Seven baskets contain bread; five of those also contain butter.
Butter appears in seven of the ten baskets, so this rule is only slightly stronger than the overall butter rate.
5. Apriori algorithm
Apriori is a classic way to find frequent item combinations before making rules.
How Apriori works
Find how often each item appears. Remove items below minimum support.
Join the remaining single items to make candidate pairs, then count them.
Join frequent groups to make larger candidates and count those too.
Split frequent itemsets into IF and THEN parts and keep rules that meet confidence.
The Apriori principle
all its subsets must be frequent.If a pair is too rare, any larger set containing that pair will also be too rare.
This rule helps Apriori save time. It can stop counting larger combinations that cannot possibly meet the minimum support.
Apriori
Uses candidate itemsets and counts their support. It is easy to explain, but can create many candidates.
FP-Growth
Compresses transaction data into a tree structure and mines frequent patterns without generating the same large candidate lists.
6. Association Rules in Python
The mlxtend library provides Apriori and association rule tools.
Using Apriori with mlxtend
Copy this example into a Python notebook.
# Install the libraries if needed:
# pip install pandas mlxtend
import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori, association_rules
# Each row is one shopping basket
transactions = [
['Bread', 'Butter', 'Milk'],
['Bread', 'Butter'],
['Bread', 'Milk'],
['Bread', 'Butter'],
['Butter', 'Milk'],
['Bread', 'Butter', 'Milk'],
['Bread'],
['Butter'],
['Bread', 'Butter'],
['Milk']
]
# Convert baskets into a one-hot table
encoder = TransactionEncoder()
encoded = encoder.fit(transactions).transform(transactions)
basket_df = pd.DataFrame(encoded, columns=encoder.columns_)
# Find itemsets that meet minimum support
frequent_itemsets = apriori(
basket_df,
min_support=0.20,
use_colnames=True
)
# Create rules and filter by confidence
rules = association_rules(
frequent_itemsets,
metric='confidence',
min_threshold=0.50
)
# Show the strongest lift values first
rules = rules.sort_values('lift', ascending=False)
print(rules[['antecedents', 'consequents',
'support', 'confidence', 'lift']])
The code uses the same ten-basket example shown above. The output contains rules that meet the chosen support and confidence settings.
TransactionEncoder turns baskets into columns of True/False values. apriori() finds frequent itemsets. association_rules() creates rules and calculates their measures.7. Where are Association Rules used?
They help discover items or events that often happen together.
Retail and e-commerce
Suggest related products, plan bundles, or organise store shelves.
Streaming and media
Find films, shows, or songs that are often selected by the same users.
Healthcare research
Explore conditions or symptoms that appear together in records. Patterns need expert review.
Website behaviour
Discover pages or actions that visitors often use in the same session.
Strengths and limitations
Know what association rules can—and cannot—tell you.
Strengths
- Finds patterns without target labels.
- Rules are often easy to read and explain.
- Can reveal unexpected item combinations.
- Useful for recommendations and planning.
Limitations
- Many possible item combinations can be slow to check.
- Rare but useful patterns may be missed by high support settings.
- High confidence does not always mean a strong relationship.
- Association does not prove cause and effect.
8. Knowledge check
Answer one question at a time. Your score and explanations will appear after the final question.
Mini challenge
Use the playground. First lower minimum support, then raise minimum confidence. Watch how the rules table changes.