기본 Apriori 결과 가지치기

Python으로 배우는 Market Basket Analysis

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Apriori와 연관 규칙

  • Apriori는 아이템셋을 가지치기합니다.
    • 최소 지지도 임계값을 적용합니다.
    • 변형 버전은 아이템 수로도 가지치기합니다.
    • 연관 규칙 자체는 알려 주지 않습니다.
  • 연관 규칙.
    • 아이템셋보다 규칙 수가 훨씬 많습니다.
    • {Bags, Boxes}: Bags -> Boxes 또는 Boxes -> Bags.
Python으로 배우는 Market Basket Analysis

연관 규칙 계산 방법

  • Apriori 결과에서 규칙 계산.
    • 높은 n, k에서는 열거가 어렵습니다.
    • Apriori의 아이템셋 가지치기를 무력화할 수 있습니다.
  • 연관 규칙 수 줄이기.
    • mlxtend 모듈이 규칙 가지치기를 제공합니다.
    • association_rules()는 빈발 아이템, 지표, 임계값을 받습니다.
Python으로 배우는 Market Basket Analysis

연관 규칙 계산 방법

# 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

가지치기의 중요성

# 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

가지치기의 중요성

# 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

규칙 집합 탐색

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

다른 지표로 가지치기

# 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...