Market Basket Analysis Using Apriori Algorithm
Market Basket Analysis Using Apriori Algorithm

Market Basket Analysis Using Apriori Algorithm

?? Introduction

Market Basket Analysis is a powerful data mining technique used by retailers to understand purchasing behavior. It helps businesses identify frequently bought-together items, optimize product placement, and drive sales strategies.

?? Goal: Use the Apriori algorithm to find associations between items in a dataset.


?? 1?? What is Market Basket Analysis?

Market Basket Analysis identifies relationships between products using Association Rule Mining (ARM).

? Example: If customers buy bread, they are likely to buy butter.

? Use Cases:

  • Retail & E-commerce: Product bundling (Amazon, Flipkart)
  • Supermarkets: Optimizing shelf placement
  • Banking & Finance: Fraud detection


?? 2?? Understanding the Apriori Algorithm

Apriori is a popular algorithm for Association Rule Mining that works in three main steps:

1?? Find Frequent Itemsets: Identify combinations of products frequently purchased together. 2?? Generate Association Rules: Create "If-Then" rules to determine relationships. 3?? Measure Rule Strength: Use Support, Confidence, and Lift to evaluate the rules.

?? Key Metrics:

? Support: How often an item appears in transactions.

? Confidence: Probability of buying item Y if item X is bought.

? Lift: Strength of association (Lift >1 means strong correlation).


?? 3?? Implementing Apriori in Python

?? Step 1: Install & Import Libraries

!pip install mlxtend  

import pandas as pd
from mlxtend.frequent_patterns import apriori, association_rules        

?? Step 2: Load & Prepare Data

# Sample Dataset (Supermarket Transactions)
data = {
    'Milk': [1, 0, 1, 1, 0, 1],
    'Bread': [1, 1, 1, 0, 1, 1],
    'Butter': [0, 1, 1, 1, 1, 1],
    'Eggs': [1, 1, 0, 1, 0, 1],
    'Cheese': [0, 0, 1, 1, 1, 0]
}

df = pd.DataFrame(data)
print(df)        

?? Binary Representation:

? 1 → Item Purchased

? 0 → Item Not Purchased


?? Step 3: Apply the Apriori Algorithm

# Generate frequent itemsets with a minimum support threshold
frequent_itemsets = apriori(df, min_support=0.3, use_colnames=True)

# Display the frequent itemsets
print(frequent_itemsets)        

?? Step 4: Generate Association Rules

# Extract association rules with confidence > 0.6
rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.6)

# Display top rules
print(rules[['antecedents', 'consequents', 'support', 'confidence', 'lift']])        

?? Example Output:

   antecedents   consequents   support   confidence   lift
--------------------------------------------------------
   (Bread)      → (Butter)      0.5         0.75       1.2
   (Milk, Bread) → (Butter)     0.4         0.85       1.4
   (Eggs)       → (Milk)        0.6         0.80       1.5        

? Bread → Butter: If a customer buys Bread, there's a 75% chance they'll buy Butter.

? Eggs → Milk: If a customer buys Eggs, they’re 80% likely to buy Milk.


?? 4?? Business Insights & Benefits

?? How Businesses Use Market Basket Analysis:

? Cross-Selling: Suggest complementary items (e.g., Amazon's “Customers Also Bought”).

? Optimizing Store Layout: Place associated items together (e.g., Chips & Dip).

? Targeted Promotions: Personalized discounts (e.g., Buy 1 Get 1 Free for related items).


?? Conclusion & Future Enhancements

? Apriori helps identify hidden shopping patterns.

? It improves sales strategies, personalized recommendations, and product placement.

? Future enhancements:

  • Use FP-Growth Algorithm for faster association mining.
  • Implement Deep Learning for more advanced recommendations.

要查看或添加评论,请登录

Ankitaa Panpatil的更多文章

社区洞察