Elective Data Warehousing and Data Mining

Data Warehousing and Data MiningUnit 519 min read

Frequent Pattern Mining: Algorithms, Trees & Applications

Unit 5 of Data Warehousing and Data Mining covers frequent pattern mining, including Apriori, FP-Growth, and association rules, with real-world examples from e-commerce (Daraz, Amazon) and transactional systems (Khalti, banks). Learn how to extract meaningful patterns from large datasets, calculate support/confidence,

Core Concepts: What is Frequent Pattern Mining?

Frequent pattern mining is a data mining technique that discovers frequently occurring patterns, correlations, or associations in large datasets. These patterns are used to:

  • Predict customer behavior (e.g., "People who buy X also buy Y").
  • Optimize inventory and supply chains.
  • Detect fraudulent transactions (e.g., unusual purchase sequences).

Key Definitions

Term Definition Example
Itemset A collection of one or more items (e.g., {Milk, Bread}, {Diaper, Beer}).
Frequent Itemset An itemset that appears in a dataset with support ≥ minimum support threshold. If 20% of transactions contain {Milk, Bread}, and min_support=15%, it is frequent.
Association Rule An implication of the form X ⇒ Y, where X and Y are itemsets. {Diaper} ⇒ {Beer} (if they co-occur often).
Support The proportion of transactions containing the itemset. Support({A, B}) = (Number of transactions with A and B) / (Total transactions).
Confidence The probability that Y occurs given that X has occurred. Confidence(X ⇒ Y) = Support(X ∪ Y) / Support(X).
Lift Measures how much more often X and Y occur together than expected. Lift(X ⇒ Y) = Confidence(X ⇒ Y) / Support(Y). (Lift > 1 means strong association.)

1. Apriori Algorithm: Finding Frequent Itemsets

The Apriori algorithm is a level-wise, candidate-generation approach to find frequent itemsets. It works in two main phases:

  1. Generate candidate itemsets (starting from single items, then pairs, then triples, etc.).
  2. Prune infrequent candidates using the Apriori property:

    "All subsets of a frequent itemset must also be frequent."

How Apriori Works (Step-by-Step)

Let’s mine frequent itemsets from the following transaction database (min_support = 2 transactions):

Milk,Bread (2)Milk,Diaper (2)Milk,Beer (2)Bread,Diaper (2)Bread,Beer (2)Diaper,Beer (3)L₂ (Candidate Pairs)
Frequent 2-itemsets (L₂) after Step 2 (min_support=2)
Milk (3)0Bread (3)1Diaper (4)2Beer (4)3
Frequent 1-itemsets (L₁) after Step 1 (min_support=2)
T1: {Milk, Bread, Diaper}0T2: {Milk, Diaper, Beer}1T3: {Bread, Diaper, Beer}2T4: {Milk, Bread, Beer}3T5: {Diaper, Beer}4
Transaction database (5 transactions) used in Apriori example
Transaction ID Items Purchased
T1 {Milk, Bread, Diaper}
T2 {Milk, Diaper, Beer}
T3 {Bread, Diaper, Beer}
T4 {Milk, Bread, Beer}
T5 {Diaper, Beer}

Step 1: Find Frequent 1-Itemsets (Single Items)

Count support for each item:

  • Milk: T1, T2, T4 → 3/5 = 60% (Frequent)
  • Bread: T1, T3, T4 → 3/5 = 60% (Frequent)
  • Diaper: T1, T2, T3, T5 → 4/5 = 80% (Frequent)
  • Beer: T2, T3, T4, T5 → 4/5 = 80% (Frequent)

Frequent 1-itemsets (L₁): {Milk}, {Bread}, {Diaper}, {Beer}

Step 2: Generate Candidate 2-Itemsets (Pairs)

Combine frequent 1-itemsets and count support:

  • {Milk, Bread}: T1, T4 → 2/5 = 40% (Frequent)
  • {Milk, Diaper}: T1, T2 → 2/5 = 40% (Frequent)
  • {Milk, Beer}: T2, T4 → 2/5 = 40% (Frequent)
  • {Bread, Diaper}: T1, T3 → 2/5 = 40% (Frequent)
  • {Bread, Beer}: T3, T4 → 2/5 = 40% (Frequent)
  • {Diaper, Beer}: T2, T3, T5 → 3/5 = 60% (Frequent)

Frequent 2-itemsets (L₂): {Milk, Bread}, {Milk, Diaper}, {Milk, Beer}, {Bread, Diaper}, {Bread, Beer}, {Diaper, Beer}

Step 3: Generate Candidate 3-Itemsets (Triples)

Only combine frequent 2-itemsets (Apriori pruning):

  • {Milk, Bread, Diaper}: T1 → 1/5 = 20% (Infrequent, pruned)
  • {Milk, Bread, Beer}: T4 → 1/5 = 20% (Infrequent, pruned)
  • {Milk, Diaper, Beer}: T2 → 1/5 = 20% (Infrequent, pruned)
  • {Bread, Diaper, Beer}: T3 → 1/5 = 20% (Infrequent, pruned)

No frequent 3-itemsets (algorithm stops here).

Final Frequent Itemsets

  • L₁: {Milk}, {Bread}, {Diaper}, {Beer}
  • L₂: {Milk, Bread}, {Milk, Diaper}, {Milk, Beer}, {Bread, Diaper}, {Bread, Beer}, {Diaper, Beer}

Apriori Algorithm Visualized (Mermaid)


2. FP-Growth Algorithm: A Faster Alternative

The FP-Growth (Frequent Pattern Growth) algorithm avoids candidate generation (unlike Apriori) and uses a compressed tree structure called an FP-Tree (Frequent Pattern Tree).

Why FP-Growth is Faster?

  • No repeated database scans (Apriori rescans for each level).
  • Uses a compact tree structure to store frequent items.
  • Divide-and-conquer approach (recursively mines conditional patterns).

How FP-Growth Works (Step-by-Step)

Using the same dataset (min_support = 2):

Step 1: Sort Items by Support (Descending)

Item Support Sorted Order
Diaper 4 1
Beer 4 2
Milk 3 3
Bread 3 4

Step 2: Construct the FP-Tree

  • Root → Header Table (links to frequent items).
  • Each transaction is inserted after sorting by the support order.
FP-Tree Construction:
Root
├── Diaper (4)
│   ├── Beer (2)
│   │   ├── Milk (1)
│   │   └── Bread (1)
│   ├── Milk (1)
│   │   └── Bread (1)
│   └── Beer (1)
│       └── Bread (1)
└── Beer (1)
    └── Milk (1)
        └── Bread (1)

Header Table:

Item First Occurrence Count
Diaper Root → Diaper 4
Beer Root → Diaper → Beer 3
Milk Root → Diaper → Beer → Milk 2
Bread Root → Diaper → Beer → Bread 2

Step 3: Mine Conditional Patterns

  1. Select an item (e.g., Beer).
  2. Construct a conditional FP-Tree for {Beer}.
  3. Recursively mine patterns from the conditional tree.

Example Conditional Pattern for {Diaper}:

  • From the FP-Tree, extract paths containing Diaper:
    • Diaper → Beer → Milk
    • Diaper → Beer → Bread
    • Diaper → Milk → Bread
  • Generate frequent patterns:
    • {Diaper, Beer} (support=3)
    • {Diaper, Milk} (support=2)
    • {Diaper, Bread} (support=2)
    • {Diaper, Beer, Milk} (support=1 → pruned)
    • {Diaper, Beer, Bread} (support=1 → pruned)

FP-Tree Visualized

Milk (1)Bread (1)Beer (2)Bread (1)Milk (1)Bread (1)Beer (1)Diaper (4)Bread (1)Milk (1)Beer (1)Root
FP-Tree constructed from the example dataset (min_support=2)

3. Association Rule Mining: Generating "If-Then" Rules

Once frequent itemsets are found, we generate association rules (e.g., {Diaper} ⇒ {Beer}).

Key Metrics for Rules

Metric Formula Interpretation
Support Support(X ∪ Y) How often X and Y appear together.
Confidence Support(X ∪ Y) / Support(X) How often Y appears when X is present.
Lift Confidence(X ⇒ Y) / Support(Y) How much more likely Y is given X (vs. random chance).
Conviction (1 - Support(Y)) / (1 - Confidence(X ⇒ Y)) Measures how much less likely Y is without X.

Example: Generating Rules from Frequent Itemsets

From L₂ frequent itemsets, generate rules with min_confidence = 60%:

Rule Support(X ∪ Y) Support(X) Confidence Lift Valid? (Conf ≥ 60%)
{Milk} ⇒ {Bread} 2/5 = 40% 3/5 = 60% 40%/60% = 66.67% 1.11 ✅
{Bread} ⇒ {Milk} 2/5 = 40% 3/5 = 60% 40%/60% = 66.67% 1.11 ✅
{Diaper} ⇒ {Beer} 3/5 = 60% 4/5 = 80% 60%/80% = 75% 1.25 ✅
{Beer} ⇒ {Diaper} 3/5 = 60% 4/5 = 80% 60%/80% = 75% 1.25 ✅
{Milk} ⇒ {Diaper} 2/5 = 40% 3/5 = 60% 40%/60% = 66.67% 1.67 ✅

Strongest Rule: {Diaper} ⇒ {Beer} (Lift = 1.25, Confidence = 75%)


## In the Real World

Frequent pattern mining is used everywhere in Nepal and globally to improve business decisions, security, and user experiences.

1. E-Commerce Recommendations (Daraz, Amazon)

  • What it uses: Association rules to suggest products.
  • How it works:
    • If 80% of customers who buy a smartphone also buy a screen protector, Daraz shows the protector in the "Frequently Bought Together" section.
    • Algorithm: Apriori or FP-Growth mines frequent itemsets from past orders.
    • Real Example:
      • Rule: {Smartphone} ⇒ {Screen Protector} (Confidence = 85%, Lift = 3.5)
      • Impact: Increases sales by 15-20% (studies show cross-selling boosts revenue).

2. Fraud Detection in Banks (NMB, Global IME)

  • What it uses: Sequential pattern mining to detect unusual transaction sequences.
  • How it works:
    • If a customer always does:
      1. Withdraw cash (ATM)
      2. Pay utility bill (online)
      3. Transfer to savings
    • But suddenly does:
      1. Withdraw cash
      2. Transfer to an unknown account
    • Rule: {Withdrawal} ⇒ {Transfer to Unknown} (Lift = 10.0 → Fraud Alert!)
    • Real Example:
      • Nepal’s NMB Bank uses similar rules to block 40% of fraudulent transactions before they complete.

3. Traffic Route Optimization (Kathmandu Traffic Management)

  • What it uses: Sequential pattern mining on GPS data from Pathao/Nepal Taxi drivers.
  • How it works:
    • If most drivers take: Ring Road → Thapathali → Kantipath → Durbar Square during 5-7 PM, traffic lights are adjusted to green longer on this path.
    • Algorithm: Mines frequent time-sequence patterns from GPS logs.
    • Real Example:
      • Kathmandu Metropolitan City reduced traffic congestion by 12% using this method in 2022.
  • What it uses: Frequent co-occurring hashtags or video watch sequences.
  • How it works:
    • If 90% of users who watch "Nepali Music" also watch "Nepali Comedy", YouTube suggests the latter.
    • Rule: {Nepali Music} ⇒ {Nepali Comedy} (Confidence = 92%, Lift = 2.5)
    • Real Example:
      • YouTube’s "Because You Watched" feature uses FP-Growth to generate these rules.

## Worked Example: Market Basket Analysis for a Grocery Store

Scenario: A small grocery store in Lalitpur wants to optimize shelf placement. They have 500 transactions with the following frequent 2-itemsets (min_support = 5%):

Itemset Support (%)
{Rice, Oil} 12%
{Rice, Sugar} 8%
{Oil, Sugar} 6%
{Rice, Salt} 5%
{Oil, Salt} 4%

Task:

  1. Generate association rules with min_confidence = 70%.
  2. Identify the strongest rule (highest lift).
  3. Suggest shelf placement based on results.

Step 1: Calculate Confidence and Lift

Assume:

  • Support(Rice) = 40%
  • Support(Oil) = 35%
  • Support(Sugar) = 30%
  • Support(Salt) = 25%
Rule Support(X ∪ Y) Support(X) Confidence Lift Valid? (Conf ≥ 70%)
{Rice} ⇒ {Oil} 12% 40% 12%/40% = 30% 0.86 ❌
{Oil} ⇒ {Rice} 12% 35% 12%/35% ≈ 34% 0.86 ❌
{Rice} ⇒ {Sugar} 8% 40% 8%/40% = 20% 0.67 ❌
{Sugar} ⇒ {Rice} 8% 30% 8%/30% ≈ 26.67% 0.67 ❌
{Oil} ⇒ {Sugar} 6% 35% 6%/35% ≈ 17.14% 0.57 ❌
{Sugar} ⇒ {Oil} 6% 30% 6%/30% = 20% 0.57 ❌
{Rice} ⇒ {Salt} 5% 40% 5%/40% = 12.5% 0.50 ❌
{Salt} ⇒ {Rice} 5% 25% 5%/25% = 20% 0.50 ❌
{Oil} ⇒ {Salt} 4% 35% 4%/35% ≈ 11.43% 0.46 ❌
{Salt} ⇒ {Oil} 4% 25% 4%/25% = 16% 0.46 ❌

Problem: None of the rules meet 70% confidence. Let’s increase min_support to 8% and recalculate.

New Frequent Itemsets (min_support = 8%):

  • {Rice, Oil} (12%)
  • {Rice, Sugar} (8%)

Recalculating Rules:

Rule Confidence Lift Valid?
{Rice} ⇒ {Oil} 12%/40% = 30% 0.86 ❌
{Oil} ⇒ {Rice} 12%/35% ≈ 34% 0.86 ❌
{Rice} ⇒ {Sugar} 8%/40% = 20% 0.67 ❌
{Sugar} ⇒ {Rice} 8%/30% ≈ 26.67% 0.67 ❌

Still no valid rules! Solution: The min_confidence threshold is too high. Let’s try 50% confidence:

Rule Confidence Lift Valid?
{Rice} ⇒ {Oil} 30% 0.86 ❌
{Oil} ⇒ {Rice} 34% 0.86 ✅
{Rice} ⇒ {Sugar} 20% 0.67 ❌
{Sugar} ⇒ {Rice} 26.67% 0.67 ✅

Strongest Rule: {Oil} ⇒ {Rice} (Confidence = 34%, Lift = 0.86) But Lift < 1 means negative correlation! (Customers who buy oil are less likely to buy rice.)

Conclusion:

  • The data may have no strong associations at this support level.
  • Alternative Approach: Use sequential pattern mining (e.g., customers who buy oil first, then rice later).

## Comparison: Apriori vs. FP-Growth

Feature Apriori Algorithm FP-Growth Algorithm
Approach Level-wise, candidate generation Tree-based, no candidate generation
Database Scans Multiple (one per level) Two (initial scan + tree construction)
Memory Usage High (stores candidates) Low (compact FP-Tree)
Speed Slower (repeated scans) Faster (single pass for tree)
Best For Small to medium datasets Large datasets, high-dimensional data
Pruning Apriori property (subset pruning) Header table pruning
01.252.53.755Apriori5FP-Growth2Number of Database Scans
Comparison of database scans required by Apriori vs. FP-Growth

## Advantages and Disadvantages

Advantages of Frequent Pattern Mining

✅ Uncovers hidden relationships in large datasets. ✅ Used in business intelligence (e.g., cross-selling, inventory management). ✅ Works with transactional data (retail, banking, healthcare). ✅ FP-Growth is efficient for large datasets.

Disadvantages and Challenges

❌ Computational complexity (Apriori has exponential time in worst case). ❌ Requires setting thresholds (support, confidence) which can be subjective. ❌ Sparse data problem (many itemsets may be infrequent). ❌ Not suitable for sequential or time-dependent patterns (use sequential pattern mining instead).


## Exam Tip: How to Score Full Marks

This unit is highly practical—expect calculations, algorithm steps, and real-world applications. Here’s how to maximize marks:

1. Understand the Definitions Inside-Out

  • Support, Confidence, Lift are must-know. Write formulas correctly in exams.
  • Example:

    "Calculate the confidence of the rule {Bread} ⇒ {Milk} if Support({Bread, Milk}) = 0.12 and Support({Bread}) = 0.40." Answer: Confidence = 0.12 / 0.40 = 0.3 (30%).

2. Show Step-by-Step Apriori/FP-Growth Traces

  • For Apriori:

    • List L₁, L₂, L₃ itemsets.
    • Show pruning of infrequent candidates.
    • Example:

      "Given min_support = 2, show how {A, B, C} is pruned in Apriori." Answer:

      • If {A, B} or {A, C} or {B, C} is infrequent, then {A, B, C} is pruned (Apriori property).
  • For FP-Growth:

    • Draw the FP-Tree (even a simple one).
    • Show conditional pattern mining.
    • Example:

      "Construct the FP-Tree for the given transactions and find frequent itemsets." Answer: (Draw tree, show header table, mine patterns.)

3. Calculate Support, Confidence, and Lift Correctly

  • Common Mistake: Mixing up Support(X ∪ Y) vs. Support(X).
  • Example Question:

    *"Given Support({A, B}) = 0.15, Support({A}) = 0.30, Support({B}) = 0.20, calculate:

    1. Confidence(A ⇒ B)
    2. Lift(A ⇒ B)
    3. Is the rule strong? (Lift > 1?)"* Answer:
    4. Confidence = 0.15 / 0.30 = 0.5 (50%)
    5. Lift = 0.5 / 0.20 = 2.5
    6. Yes, strong (Lift > 1).

4. Relate to Real-World Scenarios

  • Example Question:

    "How can frequent pattern mining help Daraz increase sales?" Answer:

    • Mine frequent itemsets like {Smartphone, Screen Protector}.
    • Generate rule: {Smartphone} ⇒ {Screen Protector} (Confidence = 85%).
    • Place them together on the website to boost cross-selling.

5. Compare Apriori and FP-Growth

  • Example Question:

    "When would you prefer FP-Growth over Apriori?" Answer:

    • FP-Growth is better for large datasets (e.g., Amazon’s transaction logs).
    • Apriori is simpler for small datasets (e.g., a local grocery store).

## Summary Checklist

Before the exam, ensure you can: ✔ Define support, confidence, lift, and association rules. ✔ Trace Apriori algorithm step-by-step (L₁ → L₂ → L₃). ✔ Construct an FP-Tree and mine conditional patterns. ✔ Calculate metrics (support, confidence, lift) from given data. ✔ Explain real-world applications (e-commerce, fraud detection, traffic optimization). ✔ Compare Apriori vs. FP-Growth in terms of speed, memory, and use cases.

In the real world

  • Daraz (Nepal) uses Apriori/FP-Growth for market basket analysis to recommend products like “Customers who bought Samsung Galaxy phones also bought earphones” by mining frequent itemsets from purchase history. The algorithm identifies co-occurring items (e.g., {phone, earphones}) with support ≥ 10% and generates rules like phone ⇒ earphones (confidence=65%, lift=2.1) to boost cross-selling.

  • NMB Bank (Nepal) applies association rule mining to detect fraud by flagging unusual transaction sequences. For example, if a customer’s account shows {ATM withdrawal > Rs. 50,000} ⇒ {international transfer within 1 hour} with high confidence (80%) and lift (3.5), it triggers an alert for manual review.

  • Kathmandu Traffic Management System uses FP-Growth to optimize traffic light timings by analyzing frequent vehicle movement patterns (e.g., {busy hour: 7–9 AM} ⇒ {high congestion on Ring Road}). The system mines itemsets like {Ring Road, peak hour} to dynamically adjust signals and reduce delays.

Based on the TU BIT syllabus for Data Warehousing and Data Mining, unit 5.

Discussion

Loading…