edSlash Labs  /  Machine Learning Lab  /  Unsupervised Learning  /  Association Rules
Algorithm 06 · Unsupervised learning · Pattern discovery

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.

Beginner friendly

The big idea

Find useful “if this, then that” patterns in data.

IF {A} → THEN {B}People who have item A in their basket may also have item B.

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.

Itemset

A group of one or more items, such as {bread, milk}.

Rule

A pattern written as an IF part and a THEN part.

Support

How often the items appear together in all transactions.

Confidence and lift

How often the THEN item appears with the IF item, and how much stronger that link is than chance baseline.

Remember: association shows that things happen together. It does not prove that one thing causes the other.

1. How do Association Rules work?

The algorithm counts item combinations, then turns useful combinations into rules.

A simple workflow

Collect transactions

Each transaction is a basket or event containing one or more items.

Find frequent itemsets

Count how many transactions contain each item combination. Keep combinations that meet minimum support.

Create possible rules

Split a frequent itemset into an IF part (antecedent) and a THEN part (consequent).

Measure and filter rules

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.

Example rule: If a basket contains bread, it may also contain butter. The rule is useful only if the numbers support it.

2. Live Association Rules playground

Change the minimum support and confidence to see which shopping rules remain.

Interactive experiment

Sample shopping transactions

Each row is one customer's basket.

12 baskets

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.

RuleSupportConfidenceLift
Press “Find rules” to begin.

How common must an item combination be?

How often should the THEN item appear when the IF items appear?

Transactions12
Frequent itemsets—
Rules found—
Best lift—
Your baskets are ready. Press “Find rules” to discover patterns.
Try this: lower support to find more patterns. Then raise confidence to keep only rules that are more reliable.

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.

Support(A → B) = P(A and B)Count of baskets with A and B ÷ total baskets

Confidence

Of the transactions that contain A, the share that also contain B.

Confidence(A → B) = P(B | A)Support(A and B) ÷ Support(A)

Lift

Compares the rule's confidence with how common B is overall.

Lift(A → B) = Confidence ÷ Support(B)Lift above 1 means a positive association.

How to read lift

Lift = 1

A and B appear together about as often as expected if unrelated.

Lift greater than 1

A and B appear together more often than the baseline suggests.

Lift below 1

A and B appear together less often than the baseline suggests.

Important: high confidence alone can be misleading when the THEN item is already very common. Lift helps compare the rule with that item's normal frequency.

4. Worked example: bread and butter

Use a tiny set of baskets to calculate the three measures.

Ten shopping baskets

BasketItems
1Bread, Butter, Milk
2Bread, Butter
3Bread, Milk
4Bread, Butter
5Butter, Milk
6Bread, Butter, Milk
7Bread
8Butter
9Bread, Butter
10Milk

Rule: Bread → Butter

Support = 5 / 10 = 50%

Five baskets contain both bread and butter.

Confidence = 5 / 7 = 71.4%

Seven baskets contain bread; five of those also contain butter.

Lift = 0.714 / 0.7 = 1.02

Butter appears in seven of the ten baskets, so this rule is only slightly stronger than the overall butter rate.

This is a useful lesson: confidence looks fairly high, but lift shows that butter is already common in the whole dataset, so the association is only slightly positive.

5. Apriori algorithm

Apriori is a classic way to find frequent item combinations before making rules.

How Apriori works

Count single items

Find how often each item appears. Remove items below minimum support.

Build pairs

Join the remaining single items to make candidate pairs, then count them.

Build larger groups

Join frequent groups to make larger candidates and count those too.

Generate rules

Split frequent itemsets into IF and THEN parts and keep rules that meet confidence.

The Apriori principle

If an itemset is frequent,
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.

Useful Python terms: 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.

0 of 10 answered

Mini challenge

Use the playground. First lower minimum support, then raise minimum confidence. Watch how the rules table changes.

Can you explain why a rule with high confidence might still have a lift close to 1?