Apriori Algorithm
Apriori Algorithm
The most famous algorithm for finding frequent itemsets and discovering meaningful association rules.
What is the Apriori Algorithm?
The Apriori Algorithm is a classic Association Rule Learning algorithm used to find frequent itemsets in transactional data and generate strong association rules from them.
Why is it Called "Apriori"?
The name comes from the Latin word "a priori", meaning "from the previous knowledge". The algorithm uses prior knowledge of frequent itemsets to find new ones.
Where is Apriori Used?
- Market Basket Analysis
- Product Recommendation Systems
- Fraud Detection
- Web Click Pattern Mining
- Healthcare Pattern Analysis
- Cross-Selling & Upselling
Key Concepts in Apriori
Itemset
A group of one or more items, e.g., {Bread, Milk}.
Transaction
A single record in the dataset, e.g., a customer's bill.
Support
How frequently an itemset appears in transactions.
Confidence
How often items in B appear when items in A are present.
Lift
How much more likely the items appear together than randomly.
Key Formulas
How Does the Apriori Algorithm Work?
Step-by-Step Process
- Set minimum support and minimum confidence.
- Calculate support for each single item.
- Remove items below minimum support.
- Generate larger itemsets (size 2, 3, ...).
- Calculate support for each itemset.
- Keep only frequent itemsets.
- Generate strong association rules using confidence and lift.
Worked Example
Consider 5 transactions in a grocery store:
| Transaction | Items |
|---|---|
| T1 | Bread, Milk |
| T2 | Bread, Butter, Milk |
| T3 | Bread, Butter |
| T4 | Milk, Butter |
| T5 | Bread, Milk, Butter |
Assume min_support = 0.4 (40%).
Step 1 — Count Item Frequencies
- Bread = 4/5 = 0.8
- Milk = 4/5 = 0.8
- Butter = 4/5 = 0.8
Step 2 — 2-Itemsets Support
- {Bread, Milk} = 3/5 = 0.6
- {Bread, Butter} = 3/5 = 0.6
- {Milk, Butter} = 3/5 = 0.6
Step 3 — 3-Itemset
- {Bread, Milk, Butter} = 2/5 = 0.4 → still frequent
Step 4 — Generate Rules
For rule: {Bread, Milk} → {Butter}
Python Example — Apriori with mlxtend
pip install pandas mlxtend
import pandas as pd
from mlxtend.frequent_patterns import apriori, association_rules
# Sample transaction dataset
data = {
"Bread": [1, 1, 1, 0, 1],
"Milk": [1, 1, 0, 1, 1],
"Butter": [0, 1, 1, 1, 1],
"Eggs": [0, 0, 1, 0, 1]
}
df = pd.DataFrame(data)
# Step 1: Find frequent itemsets
frequent_items = apriori(df, min_support=0.4, use_colnames=True)
print("Frequent Itemsets:\n", frequent_items)
# Step 2: Generate association rules
rules = association_rules(frequent_items, metric="confidence", min_threshold=0.6)
print("\nAssociation Rules:\n", rules[["antecedents", "consequents", "support", "confidence", "lift"]])
Visual Intuition
| Level | Itemset Size | Action |
|---|---|---|
| L1 | 1-itemset | Calculate support of single items |
| L2 | 2-itemset | Combine frequent items |
| L3 | 3-itemset | Expand if subsets are frequent |
| Stop | No new frequent itemsets | Generate rules |
Apriori Property (Important Concept)
- If an itemset is frequent, all its subsets must also be frequent.
- If an itemset is infrequent, all its supersets will also be infrequent.
Apriori vs FP-Growth vs ECLAT
| Algorithm | Approach | Speed | Best For |
|---|---|---|---|
| Apriori | Iterative — generates candidates | Slow | Small datasets |
| FP-Growth | Tree-based | Fast | Large datasets |
| ECLAT | Vertical data format | Medium | Quick lookups |
Real-Life Analogy
Apriori = Store Manager Observing Customers
A store manager notices that whenever someone buys pasta, they often buy pasta sauce. Apriori works the same way — it discovers such patterns automatically from huge transaction data.
Real-World Applications
Market Basket Analysis
- Identify frequent combos
- Improve store layout
E-Commerce
- "Frequently bought together"
- Recommendation systems
Banking
- Cross-product analysis
- Detect fraud patterns
Healthcare
- Drug interaction patterns
- Disease co-occurrence
Web Mining
- Click stream analysis
- Content recommendation
OTT Platforms
- Movie/show pattern analysis
- Smart suggestions
Advantages
- Easy to understand and implement.
- Discovers meaningful relationships.
- Works without labels (unsupervised).
- Useful in many industries.
- Provides interpretable rules.
Disadvantages
Common Mistakes to Avoid
Best Practices
Quick Tips
- Clean and preprocess data well.
- Test multiple support thresholds.
- Use Lift, Confidence, and Support together.
- Visualize top rules for business value.
- Switch to FP-Growth for large datasets.
- Always interpret rules in real-world context.
Importance of Apriori
Pattern Discovery
- Find hidden item relationships
- Generate useful insights
Boosts Sales
- Cross-selling strategies
- Personalized recommendations
Fraud Detection
- Unusual buying patterns
- Suspicious behavior detection
Wide Industry Use
- Retail, finance, healthcare
- Web analytics, telecom
Golden Rule
Key Takeaway
The Apriori Algorithm is the most popular method to discover association rules in transactional data. It identifies frequent itemsets and creates strong rules using Support, Confidence, and Lift. Apriori powers many real-world applications like market basket analysis and recommendation systems.