Phân tích giỏ hàng trong R
Christopher Bruffaerts
Statistician
| 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} |
Áp dụng apriori cho giao dịch:
rules = apriori(data_trx,
parameter = list(
supp = 3/7, conf = 0.6,
minlen = 2),
control = list(verbose=F)
)
Tạo dataframe từ quy tắc trích xuất
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
Tập phổ biến cho Cheese và 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
Quy tắc riêng cho 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
Quy tắc dư thừa là gì?
Một quy tắc là dư thừa nếu có một quy tắc tổng quát hơn với độ tin cậy bằng hoặc cao hơn.
Siêu quy tắc:
Một quy tắc tổng quát hơn nếu có cùng Vế phải (RHS) nhưng bớt một hoặc nhiều mục ở Vế trái (LHS).
Ví dụ:
Siêu quy tắc của {A} $\rightarrow$ {C}:
Quy tắc không dư thừa được định nghĩa là:
Tập quy tắc được sinh
rules = apriori(trans,control = list(verbose=F),
parameter = list(supp=0.05, conf=0.5, minlen=2),
appearance = list(rhs="Bread", default = "lhs"))
Tập quy tắc đã cắt tỉa (không dư thừa)
redundant_rules = is.redundant(rules)
non_redundant_rules = rules[!redundant_rules]
So sánh quy tắc trích xuất và quy tắc không dư thừa
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
Phân tích giỏ hàng trong R