Lesson 50
Apriori
Find itemsets that appear together often enough, using the fact that a rare pair cannot hide inside a frequent one. One example is on this page.
Intermediate12 min
What You Will Learn
- Idea
- Example
- When to use it
Idea
Apriori counts how often items appear in baskets. Support is that count divided by the number of baskets. If a single item is rare, every pair that contains it is rare too, so those pairs are skipped. The surviving pairs are the ones you turn into rules.
Example
baskets = [
{"bread", "milk"},
{"bread", "diaper", "beer"},
{"milk", "diaper", "beer", "cola"},
{"bread", "milk", "diaper", "beer"},
{"bread", "milk", "diaper", "cola"},
]
support = sum("diaper" in basket for basket in baskets) / len(baskets)
pair = {"diaper", "beer"}
pair_support = sum(pair <= basket for basket in baskets) / len(baskets)
print(f"support(diaper): {support:.2f}")
print(f"support(diaper, beer): {pair_support:.2f}")Output:
support(diaper): 0.80
support(diaper, beer): 0.60When to use it
- Use it on transaction lists, such as baskets, playlists, or feature sets.
- A minimum support drops rare combinations before you look at rules.
- Frequent does not mean causal. It means these items co-occur in this data.
Loading playground…