Association Rules
Association rule mining finds items that often occur together, as in market basket analysis. A rule A → B says that customers who buy A also tend to buy B. Three measures are used, where N is the number of transactions:
- Support of A → B = count(A and B) / N.
- Confidence of A → B = count(A and B) / count(A).
- Lift = confidence / support(B); a lift above 1 means a positive association.
An itemset is frequent if its support is at least the minimum support. A rule is strong if it meets both minimum support and minimum confidence.
Apriori Algorithm
Apriori uses the Apriori property: every subset of a frequent itemset must also be frequent. So if an itemset is infrequent, all its supersets are pruned.
- Scan the data and find frequent 1-itemsets L1.
- Join Lk with itself to form candidate (k+1)-itemsets.
- Prune any candidate that has an infrequent subset.
- Scan the data to count the candidates and keep the frequent ones as L(k+1).
- Repeat until no new frequent itemsets appear, then generate rules from them and keep the strong rules.
Its weakness is many scans of the database and a large number of candidates.
FP-Growth Algorithm
FP-Growth avoids candidate generation. It scans the data twice: first to find the frequent items and sort them by descending frequency, then to build a compact FP-tree in which transactions share common prefixes. Frequent patterns are then mined recursively from each item's conditional pattern base. It is usually much faster than Apriori on large data.
Definition: Support. The fraction of all transactions that contain an itemset; it measures how frequently the itemset occurs.
Table 2.2: Apriori and FP-Growth
| Point | Apriori | FP-Growth |
|---|---|---|
| Candidates | Generated and tested | None |
| Database scans | One per level | Two |
| Structure | Candidate sets | FP-tree |
| Speed on large data | Slow | Faster |
| Memory | Low per level | Needs the tree in memory |
Example 2.2: Frequent itemsets and a rule
Problem: Five transactions are T1 {Bread, Milk}, T2 {Bread, Diaper, Beer, Eggs}, T3 {Milk, Diaper, Beer, Cola}, T4 {Bread, Milk, Diaper, Beer} and T5 {Bread, Milk, Diaper, Cola}. With minimum support count 3, find the frequent itemsets and the confidence of Beer → Diaper.
Solution:
- L1: Bread 4, Milk 4, Diaper 4, Beer 3 are frequent; Eggs 1 and Cola 2 are dropped.
- Pairs: Bread-Milk 3, Bread-Diaper 3, Milk-Diaper 3, Diaper-Beer 3 are frequent; Bread-Beer 2 and Milk-Beer 2 are not.
- The only triple candidate, Bread-Milk-Diaper, has count 2, so L3 is empty.
- Confidence of Beer → Diaper = count(Beer and Diaper) / count(Beer) = 3 / 3 = 100 percent; support = 3 / 5 = 60 percent.
Result: Four frequent items and four frequent pairs; the rule Beer → Diaper has support 60 percent and confidence 100 percent (lift 1.25).
T = [{"Bread", "Milk"}, {"Bread", "Diaper", "Beer", "Eggs"},
{"Milk", "Diaper", "Beer", "Cola"},
{"Bread", "Milk", "Diaper", "Beer"},
{"Bread", "Milk", "Diaper", "Cola"}]
minsup = 3
def count(s):
return sum(1 for t in T if set(s) <= t)
items = sorted({i for t in T for i in t})
level = [frozenset([i]) for i in items if count([i]) >= minsup]
k = 1
while level:
names = sorted("+".join(sorted(s)) for s in level)
print("L%d:" % k, names)
cands = {a | b for a in level for b in level
if len(a | b) == k + 1}
level = [c for c in cands if count(c) >= minsup]
k += 1
sup = count(["Beer", "Diaper"]) / len(T)
conf = count(["Beer", "Diaper"]) / count(["Beer"])
print("Beer -> Diaper support", sup, "confidence", conf)
print("Lift:", conf / (count(["Diaper"]) / len(T)))
Output
L1: ['Beer', 'Bread', 'Diaper', 'Milk']
L2: ['Beer+Diaper', 'Bread+Diaper', 'Bread+Milk', 'Diaper+Milk']
Beer -> Diaper support 0.6 confidence 1.0
Lift: 1.25
Common mistakes
- Forgetting to prune candidates that have an infrequent subset.
- Mixing up support (about the whole rule) with confidence (about the antecedent).
- Choosing a very low minimum support and getting millions of rules.
- Treating a high-confidence rule as causal; Beer does not cause Diaper.
Key takeaways
- Support = count(A and B) / N; confidence = count(A and B) / count(A).
- Apriori property: subsets of frequent itemsets are frequent.
- Apriori generates candidates; FP-Growth builds an FP-tree and does not.
- Lift above 1 means a positive association.
Practice questions
- State the Apriori property.
- How is confidence of A → B calculated?
- What advantage does FP-Growth have?
Answers to the practice questions
- Every subset of a frequent itemset is also frequent.
- count(A and B) divided by count(A).
- It avoids candidate generation and needs only two scans.
Sources
- Official BCA syllabus Data Warehousing and Mining, course code 25BCA406DS03, Unit II
