„Pokud toto, pak ono“ s apriori

Market Basket Analysis v R

Christopher Bruffaerts

Statistician

Rekapitulace extrahovaných pravidel (1)

TID Transakce
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}

Aplikace apriori na transakce:

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

Rekapitulace extrahovaných pravidel (2)

Vytvoření datového rámce s extrahovanými pravidly

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
Market Basket Analysis v R

Výskyt frekventovaných sad položek

Frekventované sady položek pro Cheese a 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
Market Basket Analysis v R

Výskyt extrahovaných pravidel

Specifická pravidla pro 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
Market Basket Analysis v R

Redundantní pravidla

Co je redundantní pravidlo?

Pravidlo je redundantní, pokud existuje obecnější pravidlo se stejnou nebo vyšší spolehlivostí.

Nadřazené pravidlo:

Pravidlo je obecnější, pokud má stejnou pravou stranu (RHS), ale z levé strany (LHS) je odebrána jedna nebo více položek.

Příklad:

Nadřazená pravidla pro {A} $\rightarrow$ {C}:

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

Neredundantní pravidla jsou definována takto:

  • všechna ostatní pravidla jsou jejich nadřazenými pravidly
  • všechna ostatní pravidla mají nižší spolehlivost
Market Basket Analysis v R

Redundance pravidel (1)

Sada vygenerovaných pravidel

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

Sada ořezaných pravidel (neredundantní)

redundant_rules = is.redundant(rules)
non_redundant_rules = rules[!redundant_rules]
Market Basket Analysis v R

Redundance pravidel (2)

Porovnání extrahovaných a neredundantních pravidel

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
Market Basket Analysis v R

Pojďme dodržovat pravidla!

Market Basket Analysis v R

Preparing Video For Download...