Customers who buy bread also buy butter
An association rule is an if-then pattern found in records, such as "if a basket holds bread, it also holds butter". Shops, libraries and websites mine rules like this from their sales and loans.
A rule is only useful if you can rate it. You will learn two ratings, confidence and lift, and how to tell a real link from a coincidence.
By the end of this lesson
Note
I can turn a frequent pattern into a rule and rate it with confidence and lift.
Key words
| Word | What it means | Example |
|---|---|---|
| Rule | an if-then pattern between two itemsets | bread → butter |
| Antecedent | the "if" side of a rule | |
| Consequent | the "then" side of a rule | |
| Confidence | the share of baskets with the antecedent that also hold the consequent | |
| Lift | confidence divided by the support of the consequent |
The cells students fill in are left blank in this preview.
Confidence is how often the rule holds
A rule has two sides. Written bread → butter, bread is the antecedent and butter is the consequent.
Confidence answers one question: of the baskets that hold the antecedent, what share also hold the consequent? Count the baskets with both, then divide by the baskets with the antecedent alone.
Worked example: a rule and its reverse
| Rule | Baskets with the "if" | Baskets with both | Confidence |
|---|---|---|---|
| bread → butter | 6 | 4 | 4 / 6 = 0.667 |
| butter → bread | 4 | 4 | 4 / 4 = 1.0 |
| milk → eggs | 6 | 3 | 3 / 6 = 0.5 |
| eggs → milk | 4 | 3 | 3 / 4 = 0.75 |
The cells students fill in are left blank in this preview.
Work out confidence in both directions
baskets = [
["bread", "milk", "eggs"], ["bread", "butter"], ["milk", "eggs", "tea"],
["bread", "milk", "butter"], ["bread", "milk", "eggs", "butter"],
["milk", "tea"], ["bread", "eggs"], ["bread", "milk", "butter"],
]
def count(items):
total = 0
for basket in baskets:
found = True
for item in items:
if item not in basket:
found = False
if found:
total = total + 1
return total
def confidence(first, then):
return count([first, then]) / count([first])
print("bread then butter:", round(confidence("bread", "butter"), 3))
print("butter then bread:", round(confidence("butter", "bread"), 3))
# Add a line for milk then eggs. Work out the answer by hand first.
Lift compares a rule with chance
Confidence can be high only because the consequent is common. If 75 baskets in 100 hold milk, then "anything → milk" is right 75 times in 100 by luck.
Lift removes the luck: confidence divided by the support of the consequent. A lift of 1 means no link. Above 1, the items go together more than chance. Below 1, less.
Worked example: three rules rated
| Rule | Confidence | Support of the consequent | Lift | Reading |
|---|---|---|---|---|
| bread → butter | 0.667 | 0.5 | 1.333 | Together more than chance |
| milk → eggs | 0.5 | 0.5 | 1.0 | No link |
| bread → milk | 0.667 | 0.75 | 0.889 | Together less than chance |
The cells students fill in are left blank in this preview.
Rate bread → milk with Python
docker exec jupyter python3 -c "print(round(4 / 6, 3), round((4 / 6) / (6 / 8), 3))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think a high confidence means a strong rule. Bread → milk has a confidence of 0.667, but milk is in 75% of all baskets, so the rule does worse than chance. Always read confidence beside lift.
Testing a rule on the lab's days
The lab's bike-hire days make a good test. Take the rule "sunny → busy", where busy means 300 hires or more.
The next command counts the sunny days that were busy. It divides by the sunny days for the confidence. Then it divides by the share of busy days for the lift.
Rate the rule sunny → busy
docker exec jupyter python3 -c "import csv; days = list(csv.DictReader(open('hires.csv'))); sunny = [d for d in days if d['weather'] == 'sunny']; busy = [d for d in days if int(d['hires']) >= 300]; both = [d for d in sunny if int(d['hires']) >= 300]; conf = len(both) / len(sunny); print(round(conf, 3), round(conf / (len(busy) / len(days)), 3))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Going further
A rule seen in only one basket can have a huge lift and mean nothing. Find such a rule in the stationery baskets of the assignment, then explain why a minimum support is worked out before any rule is rated.
Then try the rule weekend → busy on the lab data and compare its lift with sunny → busy.