Association Rules

Association rules are "if-then" statements that help uncover relationships between items in large transactional datasets. A typical rule looks like: {Bread, Butter} -> {Jam}, meaning customers who buy bread and butter also tend to buy jam.

Support

Support measures how frequently an itemset appears in the dataset:

Support(A) = (Transactions containing A) / (Total transactions)

Confidence

Confidence measures how often the rule has been found to be true - i.e., given that A was purchased, how likely is B to also be purchased:

Confidence(A -> B) = Support(A and B) / Support(A)

The Apriori Algorithm

The Apriori algorithm is the most common method for mining association rules. It works on the principle that if an itemset is frequent, then all of its subsets must also be frequent - this lets it efficiently prune the search space instead of checking every possible combination.

Implementing in Python

from mlxtend.frequent_patterns import apriori, association_rules

frequent_itemsets = apriori(df, min_support=0.2, use_colnames=True)
rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.6)

print(rules[["antecedents", "consequents", "support", "confidence"]])
A high confidence score alone can be misleading if the consequent item is already very popular on its own - this is exactly the gap that the Lift Ratio, covered later in this section, is designed to fix.

Coming Up Next

Before diving into lift ratio, it helps to revisit the underlying probability concept that confidence is actually built on - Conditional Probability.

Ready to Master Data Science?

Join Uncodemy's Data Science Course and build real, job-ready skills with expert mentors.

Explore Course