बेसिक Apriori रिज़ल्ट्स प्रूनिंग

Python में Market Basket Analysis

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Apriori और association rules

  • Apriori आइटमसेट्स को prune करता है.
    • न्यूनतम सपोर्ट थ्रेशोल्ड लागू करता है.
    • बदला हुआ वर्ज़न आइटम की संख्या से भी prune कर सकता है.
    • यह हमें association rules के बारे में नहीं बताता.
  • Association rules.
    • आइटमसेट्स से कहीं ज़्यादा association rules होते हैं.
    • {Bags, Boxes}: Bags -> Boxes या Boxes -> Bags.
Python में Market Basket Analysis

Association rules कैसे निकालें

  • Apriori रिज़ल्ट्स से rules निकालना.
    • बड़े n और k के लिए गिनना मुश्किल.
    • Apriori द्वारा itemset pruning उलट सकता है.
  • Association rules कम करना.
    • mlxtend मॉड्यूल rules को prune करने के तरीके देता है.
    • association_rules() frequent items, metric, और threshold लेता है.
Python में Market Basket Analysis

Association rules कैसे निकालें

# Import Apriori algorithm
from mlxtend.frequent_patterns import apriori, association_rules

# Load one-hot encoded novelty gifts data
onehot = pd.read_csv('datasets/online_retail_onehot.csv')

# Apply Apriori algorithm
frequent_itemsets = apriori(onehot, 
                            use_colnames=True, 
                            min_support=0.0001)
# Compute association rules
rules = association_rules(frequent_itemsets,
                          metric = "support", 
                          min_threshold = 0.0)
Python में Market Basket Analysis

Pruning का महत्व

# Print the rules.
print(rules)
                               antecedents  ... conviction
0      (CARDHOLDER GINGHAM CHRISTMAS TREE)  ...      inf
...
79505      (SET OF 3 HEART COOKIE CUTTERS)  ... 1.998496
# Print the frequent itemsets.
print(frequent_itemsets)
       support                                           itemsets
0     0.000752                   ( 50'S CHRISTMAS GIFT BAG LARGE)
...
4707  0.000752                  (PIZZA PLATE IN BOX, CHRISTMAS ...
Python में Market Basket Analysis

Pruning का महत्व

# Compute association rules
rules = association_rules(frequent_itemsets,
                          metric = "support", 
                          min_threshold = 0.001)

# Print the rules.
print(rules)
                   antecedents      conviction  
0  (BIRTHDAY CARD, RETRO SPOT)  ...  2.977444 
1    (JUMBO BAG RED RETROSPOT)  ...  1.247180
Python में Market Basket Analysis

Rules के सेट को explore करना

print(rules.columns)
Index(['antecedents', 'consequents', 'antecedent support',
       'consequent support', 'support', 'confidence', 'lift', 'leverage',
       'conviction'],
      dtype='object')
print(rules[['antecedents','consequents']])
                   antecedents                  consequents
0    (JUMBO BAG RED RETROSPOT)  (BIRTHDAY CARD, RETRO SPOT)
1  (BIRTHDAY CARD, RETRO SPOT)    (JUMBO BAG RED RETROSPOT)
Python में Market Basket Analysis

अन्य metrics से pruning

# Compute association rules
rules = association_rules(frequent_itemsets,
                          metric = "antecedent support", 
                          min_threshold = 0.002)

# Print the number of rules.
print(len(rules))
3899
Python में Market Basket Analysis

अभ्यास करते हैं!

Python में Market Basket Analysis

Preparing Video For Download...