Basket size là gì
library(arules) library(arulesViz) library(reshape2) library(dplyr)
folder <- "D:/2.R/1.Datamining/1.Basket analysis/" baset.raw.test <- read.csv(file = paste(folder,"basket.raw.csv",sep = "")) baset.raw.test %>% head() ## cif item value ## 1 192852 banca 1 ## 2 3106491 banca 1 ## 3 1998832 banca 1 ## 4 2251209 banca 1 ## 5 2169657 banca 1 ## 6 84728 banca 1
baset.raw <- read.transactions(file = paste(folder,"basket.raw.csv",sep = ""), format = "single", sep = ",", cols = c("cif","item")) baset.raw %>% summary() ## transactions as itemMatrix in sparse format with ## 42264 rows (elements/itemsets/transactions) and ## 33 columns (items) and a density of 0.05489449 ## ## most frequent items: ## tgkkh i2b gd_noibo card_debit pro_auto (Other) ## 40940 10627 5046 4076 3907 11966 ## ## element (itemset/transaction) length distribution: ## sizes ## 1 2 3 4 5 6 7 8 9 10 11 12 ## 25388 8971 3873 1711 951 551 362 190 136 66 32 20 ## 13 14 15 16 ## 7 4 1 1 ## ## Min. 1st Qu. Median Mean 3rd Qu. Max. ## 1.000 1.000 1.000 1.812 2.000 16.000 ## ## includes extended item information - examples: ## labels ## 1 banca ## 2 bao_lanh_bao_hanh ## 3 bao_lanh_du_thau ## ## includes extended transaction information - examples: ## transactionID ## 1 1000 ## 2 1000019 ## 3 100003
rule <- apriori(data = baset.raw, parameter = list(support = 0.01, #Minimum support level to filter the transaction. # Nếu mức support nhỏ nhất là (10%) thì có nghĩa là nếu khách hàng mua 1 sản phẩm A # mà ít hơn 10% thì sản phẩm đó sẽ không đc coi là 1 rules. confidence = 0.5, # Minimum confidence level to filter the transaction- # Mức độ tin cậy tới thiểu để lọc các giá trị maxlen = 9, # Tìm kiếm mối quan hệ tối đa của 2 sản phẩm. (A ==> B) minlen = 1) # ,appearance = list(rhs = c("Pro1")) ) ## Apriori ## ## Parameter specification: ## confidence minval smax arem aval originalSupport support minlen maxlen ## 0.5 0.1 1 none FALSE TRUE 0.01 1 9 ## target ext ## rules FALSE ## ## Algorithmic control: ## filter tree heap memopt load sort verbose ## 0.1 TRUE TRUE FALSE TRUE 2 TRUE ## ## Absolute minimum support count: 422 ## ## set item appearances ...[0 item(s)] done [0.00s]. ## set transactions ...[33 item(s), 42264 transaction(s)] done [0.00s]. ## sorting and recoding items ... [17 item(s)] done [0.00s]. ## creating transaction tree ... done [0.01s]. ## checking subsets of size 1 2 3 4 done [0.00s]. ## writing ... [62 rule(s)] done [0.00s]. ## creating S4 object ... done [0.00s].# Bước 3: Kiểm tra các rule có chỉ số lift là cao nhất as(rule,"data.frame") %>% arrange(desc(lift)) %>% head() ## rules support confidence lift ## 1 {fx,tgkkh} => {gd_quocte} 0.01258754 0.8404423 61.242164 ## 2 {gd_quocte} => {fx} 0.01258754 0.9172414 61.145567 ## 3 {fx} => {gd_quocte} 0.01258754 0.8391167 61.145567 ## 4 {gd_quocte,tgkkh} => {fx} 0.01258754 0.9172414 61.145567 ## 5 {card_credit,i2b} => {gd_noibo} 0.01071834 0.6937213 5.810431 ## 6 {card_credit,i2b,tgkkh} => {gd_noibo} 0.01071834 0.6937213 5.810431
# Bước 3: Loại bỏ những rule trùng lặp. subset.matrix <- is.subset(rule,rule) # Tạo matrix đánh giá các rule #subset.matrix[1:3,1:3] # Kiểm tra matrix vuông con 3x3. # Là ma trận vuông cấp nxn ( n là số rule được tạo), nên các giá trị bị trùng lặp qua trường chéo chính nhận giá trị (TRUE). # nên ta đưa các giá trị phía dước đường chéo chính về giá trị NA. subset.matrix[lower.tri(subset.matrix,diag = T)] <- NA # Lấy toàn bộ những giao dịch là tập con của giao dịch khác. Tạo là tập redundant. redundant <- colSums(subset.matrix,na.rm=T) >= 1 # Loại bỏ những rule trùng (loại bỏ khỏi rule ban đầu tập redundant) rules <- rule[!redundant] Định nghĩa về rule trùng:
rule.final <- rules rule.final %>% inspect() ## lhs rhs support confidence lift ## 1 {} => {tgkkh} 0.96867310 0.9686731 1.000000 ## 6 {gd_quocte} => {fx} 0.01258754 0.9172414 61.145567 ## 13 {pro_none} => {i2b} 0.01802953 0.5277008 2.098687 ## 15 {card_credit} => {gd_noibo} 0.01925989 0.6237548 5.224410 ## 16 {card_credit} => {i2b} 0.01545050 0.5003831 1.990044 ## 21 {gd_noibo} => {i2b} 0.06009843 0.5033690 2.001918
sub.rule1 <- subset(rule.final,(lhs %in% c("card_credit"))) sub.rule1 %>% inspect() ## lhs rhs support confidence lift ## 15 {card_credit} => {gd_noibo} 0.01925989 0.6237548 5.224410 ## 16 {card_credit} => {i2b} 0.01545050 0.5003831 1.990044
sub.rule2 <- subset(rule.final,(lhs %in% c("gd_noibo"))) sub.rule2 %>% inspect() ## lhs rhs support confidence lift ## 21 {gd_noibo} => {i2b} 0.06009843 0.503369 2.001918
as(rule.final,"data.frame") %>% arrange(desc(lift)) %>% as.data.frame() ## rules support confidence lift ## 1 {gd_quocte} => {fx} 0.01258754 0.9172414 61.145567 ## 2 {card_credit} => {gd_noibo} 0.01925989 0.6237548 5.224410 ## 3 {pro_none} => {i2b} 0.01802953 0.5277008 2.098687 ## 4 {gd_noibo} => {i2b} 0.06009843 0.5033690 2.001918 ## 5 {card_credit} => {i2b} 0.01545050 0.5003831 1.990044 ## 6 {} => {tgkkh} 0.96867310 0.9686731 1.000000
plot(rule.final,method = "graph") |