Các chỉ số trong phân tích giỏ hàng

Phân tích giỏ hàng trong R

Christopher Bruffaerts

Statistician

Chỉ số dùng để trích xuất luật

TID Giao dịch
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}

Mục tiêu: Trích xuất luật kết hợp

Ví dụ:

  • {Bread} $\rightarrow$ {Butter}
    • Bread = "Tiền đề"
    • Butter = "Hậu đề"
  • {Butter, Cheese} $\rightarrow$ {Wine}

Chỉ số: Support, confidence, lift,...

Phân tích giỏ hàng trong R

Thước đo support

TID Giao dịch
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}

Support: "mức độ phổ biến của một tập mục"

  • supp(X) = Tỷ lệ giao dịch chứa tập mục X.
  • supp(X $\cup$ Y) = Tỷ lệ giao dịch chứa cả X và Y.

Ví dụ:

  • supp({Bread}) = 3/7 = 42%
  • supp({Bread} $\cup$ {Butter}) = 3/7 = 42%
Phân tích giỏ hàng trong R

Thước đo confidence

TID Giao dịch
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}

Confidence: "tần suất luật đúng"

conf(X $\rightarrow$ Y) = supp(X $\cup$ Y) / supp(X)

Confidence cho biết tỷ lệ Y được mua cùng X.

Ví dụ:

X = {Bread}

Y = {Butter}

conf(X $\rightarrow$ Y) = $\frac{3/7}{3/7}$ = 100%

Phân tích giỏ hàng trong R

Thước đo lift

TID Giao dịch
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}

Lift: "mức độ mạnh của liên kết"

lift(X $\rightarrow$ Y) = $\dfrac{supp(X \cup Y)}{supp(X) \times supp(Y)}$

  • Lift > 1: Y có khả năng được mua cùng X
  • Lift < 1: Y ít có khả năng được mua nếu X được mua

Ví dụ:

X = {Bread}; Y = {Butter}

lift(X $\rightarrow$ Y) = $\frac{3/7}{(3/7)*(6/7)} = \frac{7}{6}$ ~ 1.16

Phân tích giỏ hàng trong R

Hàm apriori cho tập mục phổ biến

library(arules)
# Frequent itemsets
supp.cw = apriori(trans, # the transactional dataset
                  # Parameter list
                  parameter=list(
                    # Minimum Support
                    supp=0.2,
                    # Minimum Confidence
                    conf=0.4,
                    # Minimum length
                    minlen=2,
                    # Target
                    target="frequent itemsets"),
                  # Appearence argument
                  appearance = list(
                    items = c("Cheese","Wine"))
                 )
Phân tích giỏ hàng trong R

Hàm apriori cho luật

library(arules)
# Rules
rules.b.rhs = apriori(trans, # the transactional dataset
                  # Parameter list
                  parameter=list(
                    # Minimum Support
                    supp=0.2,
                    # Minimum Confidence
                    conf=0.4,
                    # Minimum length
                    minlen=2,
                    # Target
                    target="rules"),
                  # Appearence argument
                   appearance = list(
                     rhs = "Butter",
                    default = "lhs")
                 )
Phân tích giỏ hàng trong R

Tập mục phổ biến với apriori

TID Giao dịch
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}

Truy xuất tập mục phổ biến

supp.all = apriori(trans, 
           parameter=list(supp=3/7,
           target="frequent itemsets"))        
inspect(head(sort(supp.all,by="support"),3))
    items          support   count
[1] {Butter}       0.8571429 6    
[2] {Wine}         0.7142857 5    
[3] {Cheese}       0.5714286 4
Phân tích giỏ hàng trong R

Xem chỉ số confidence và lift

TID Giao dịch
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}

Truy xuất luật

# Luật với "Butter" ở vế phải
rules.b.rhs = apriori(trans, 
                 parameter=list(
                    minlen=2,
                    target="rules"), 
                 appearance = list(
                     rhs="Butter",
                    default = "lhs")
           )
inspect(head(sort(rules.b.rhs,by="lift")), 5)
Phân tích giỏ hàng trong R

Xem chỉ số confidence và lift

TID Giao dịch
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}

Truy xuất luật

    lhs                    rhs      support  confidence lift  count
[1] {Bread}             => {Butter} 0.42      1.0        1.16     3    
[2] {Bread,Cheese}      => {Butter} 0.14      1.0        1.16     1    
[3] {Bread,Wine}        => {Butter} 0.28      1.0        1.16     2    
[4] {Bread,Cheese,Wine} => {Butter} 0.14      1.0        1.16     1    
[5] {Wine}              => {Butter} 0.57      0.8        0.93     4
Phân tích giỏ hàng trong R

Ayo berlatih!

Phân tích giỏ hàng trong R

Preparing Video For Download...