apriori로 "If this then that"

R로 배우는 Market Basket Analysis

Christopher Bruffaerts

Statistician

추출 규칙 요약 (1)

TID 거래 내역
1 {Bread, Butter, Cheese, Wine}
2 {Bread, Butter, Wine}
3 {Bread, Butter}
4 {Butter, Cheese, Wine}
5 {Butter, Cheese}
6 {Cheese, Wine}
7 {Butter, Wine}

거래에 apriori 적용:

rules = apriori(data_trx,
                    parameter = list(
                      supp = 3/7, conf = 0.6,
                      minlen = 2),
                    control = list(verbose=F)
)
R로 배우는 Market Basket Analysis

추출 규칙 요약 (2)

추출 규칙으로 데이터프레임 생성

df_rules = as(rules, "data.frame")
df_rules
                 rules   support confidence      lift count
1  {Bread} => {Butter} 0.4285714  1.0000000 1.1666667     3
2   {Cheese} => {Wine} 0.4285714  0.7500000 1.0500000     3
3   {Wine} => {Cheese} 0.4285714  0.6000000 1.0500000     3
4 {Cheese} => {Butter} 0.4285714  0.7500000 0.8750000     3
5   {Wine} => {Butter} 0.5714286  0.8000000 0.9333333     4
6   {Butter} => {Wine} 0.5714286  0.6666667 0.9333333     4
R로 배우는 Market Basket Analysis

빈발 항목집합의 지정(Appearance)

Cheese와 Wine의 빈발 항목집합

supp_cheese_wine = 
    apriori(trans, 
        parameter = list(
          target = "frequent itemsets",
          supp = 3/7),
        appearance = list(
          items = c("Cheese",  "Wine"))
)
inspect(supp_cheese_wine)
    items         support   count
[1] {Cheese}      0.5714286 4    
[2] {Wine}        0.7142857 5    
[3] {Cheese,Wine} 0.4285714 3
R로 배우는 Market Basket Analysis

추출 규칙의 지정(Appearance)

Cheese 대상 규칙

rules_cheese_rhs = apriori(data = trans, 
                   parameter = list(supp=3/7,conf=0.2, minlen=2),
                   appearance = list(rhs="Cheese"),
                   control = list (verbose=F))
inspect(rules_cheese_rhs)
    lhs         rhs      support   confidence lift  count
[1] {Wine}   => {Cheese} 0.4285714 0.6        1.050 3    
[2] {Butter} => {Cheese} 0.4285714 0.5        0.875 3
R로 배우는 Market Basket Analysis

중복 규칙

중복 규칙이란?

동일하거나 더 높은 신뢰도를 가진 더 일반적인 규칙이 있으면 해당 규칙은 중복입니다.

상위 규칙(Super-rule):

RHS가 같고 LHS에서 하나 이상 항목이 제거되면 더 일반적인 규칙입니다.

예시:

{A} $\rightarrow$ {C}의 상위 규칙:

  • {A, B} $\rightarrow$ {C}
  • {A, B, D} $\rightarrow$ {C}

비중복 규칙의 정의:

  • 다른 모든 규칙이 그 규칙의 상위 규칙이거나
  • 다른 모든 규칙의 신뢰도가 더 낮음
R로 배우는 Market Basket Analysis

규칙 중복성 (1)

생성된 규칙 집합

rules = apriori(trans,control = list(verbose=F),
                parameter = list(supp=0.05, conf=0.5, minlen=2),
                appearance = list(rhs="Bread", default = "lhs"))

가지치기 후 규칙 집합(비중복)

redundant_rules = is.redundant(rules)
non_redundant_rules = rules[!redundant_rules]
R로 배우는 Market Basket Analysis

규칙 중복성 (2)

추출 규칙 vs 비중복 규칙 비교

inspect(rules) 
    lhs                     rhs     support   confidence lift     count
[1] {Butter}             => {Bread} 0.4285714 0.5        1.166667 3    
[2] {Butter,Wine}        => {Bread} 0.2857143 0.5        1.166667 2    
[3] {Butter,Cheese,Wine} => {Bread} 0.1428571 0.5        1.166667 1  
inspect(non_redundant_rules)
    lhs         rhs     support   confidence lift     count
[1] {Butter} => {Bread} 0.4285714 0.5        1.166667 3
R로 배우는 Market Basket Analysis

규칙을 따져 봅시다!

R로 배우는 Market Basket Analysis

Preparing Video For Download...