Apriori-信心出乎意料的结果

时间:2018-10-30 19:11:10

标签: r apriori

我下面有两个数据集,ItemListFiltered和ItemListAll。 ItemListFiltered只是ItemListAll的子集。我无法确认apriori函数的结果。

ItemListAll返回一条规则...

{V2=C15OMB3CH, V3=C15KRLMRO} => {V1=C15MORL} with a confidence of 37%

ItemListFiltered返回规则...

{V1=C15KRLMRO, V3=C15OMB3CH} => {V2=C15MORL} with a confidence of 80%

我原本以为的是信心计算...

Confidence = Count of Orders from X&Y / Count of Orders from X

我假设X是(=>)的左侧,Y是(=>)的右侧

C15OMB3CH,C15KRLMRO和C15MORL在一起有4个订单,而C15OMB3CH和C15KRLMRO则有11个订单。 4/11 = 37%

这两个人如何才能恢复信心?

R 3.5.1代码

library(Matrix)
library(arules)
library(plyr)
library(dplyr)
library(gridExtra)

dataset <- read.csv("ItemListAll.csv", header=TRUE, sep=",")
#dataset <- read.csv("ItemListFiltered.csv", header=TRUE, sep=",")

df_itemList <- ddply(dataset,c("SALESID"),function(df1)paste(df1$ITEMID));
df_itemList$V1 <- as.factor(df_itemList$V1);
df_itemList$V2 <- as.factor(df_itemList$V2);
df_itemList$V3 <- as.factor(df_itemList$V3);

basket_rules <- apriori(select(df_itemList,c("V1","V2","V3")), parameter = list(sup=0.001,conf=0.1,target="rules", minlen=3));

df_basket <- as(basket_rules,"data.frame");
df_basket$support <- ceiling(df_basket$support * 100);
df_basket$confidence<- ceiling(df_basket$confidence * 100);
df_basket$lift<- round(df_basket$lift, digits = 2);
df_basket <- df_basket[rev(order(df_basket$support)),];
View(df_basket);

ItemListFiltered.csv

SALESID     ITEMID
SO0973259-1 C15ABPRRO
SO0972779-1 C15LGS5M
SO0971629-1 C15MORL
SO0972734-1 C15MORL
SO0973561-3 C15MORL
SO0973561-4 C15MORL
SO0974783-1 C15PECHRO
SO0974783-2 C15PECHRO
SO0973561-1 C15RPDERO
SO0973561-2 C15RPDERO
SO0974466-1 C15RPDERO
SO0973259-1 C15OMB3CH
SO0972779-1 C15OMB3CH
SO0971629-1 C15OMB3CH
SO0972734-1 C15OMB3CH
SO0973561-3 C15OMB3CH
SO0973561-4 C15OMB3CH
SO0974783-1 C15OMB3CH
SO0974783-2 C15OMB3CH
SO0973561-1 C15OMB3CH
SO0973561-2 C15OMB3CH
SO0974466-1 C15OMB3CH
SO0973259-1 C15KRLMRO
SO0972779-1 C15KRLMRO
SO0971629-1 C15KRLMRO
SO0972734-1 C15KRLMRO
SO0973561-3 C15KRLMRO
SO0973561-4 C15KRLMRO
SO0974783-1 C15KRLMRO
SO0974783-2 C15KRLMRO
SO0973561-1 C15KRLMRO
SO0973561-2 C15KRLMRO
SO0974466-1 C15KRLMRO

ItemListAll.csv

SALESID     ITEMID
SO0971629-1 C15KRLMRO
SO0971629-1 C15MORL
SO0971629-1 C15OMB3CH
SO0971841-2 C15ARBSIM
SO0971841-2 C15LADCRO
SO0971841-2 C15MORL
SO0972404-1 C15ABPRRO
SO0972404-1 C15CAOBG
SO0972404-1 C15PV20
SO0972564-3 C15ABPRRO
SO0972564-3 C15CAINM550
SO0972564-3 C15DLMT
SO0972710-1 C15DLMT
SO0972710-1 C15LGS5M
SO0972710-1 C15OMB3CH
SO0972734-1 C15KRLMRO
SO0972734-1 C15MORL
SO0972734-1 C15OMB3CH
SO0972744-1 C15CAINM550
SO0972744-1 C15KRLMRO
SO0972744-1 C15LGS5M
SO0972779-1 C15KRLMRO
SO0972779-1 C15LGS5M
SO0972779-1 C15OMB3CH
SO0972882-1 C15CAOBG
SO0972882-1 C15LADCRO
SO0972882-1 C15RPDERO
SO0972914-2 C15KRLMRO
SO0972914-2 C15LADCRO
SO0972914-2 C15LGS5M
SO0972948-1 C15ARBSIM
SO0972948-1 C15EPCNWCB
SO0972948-1 C15LGS5M
SO0972981-1 C15CAOBG
SO0972981-1 C15LADCRO
SO0972981-1 C15PECHRO
SO0972992-1 C15CAOBG
SO0972992-1 C15OMB3CH
SO0972992-1 C15PECHRO
SO0972995-2 C15CAOBG
SO0972995-2 C15MORL
SO0972995-2 C15OMB3CH
SO0973002-1 C15KRLMRO
SO0973002-1 C15LADCRO
SO0973002-1 C15MORL
SO0973010-1 C15ABPRRO
SO0973010-1 C15CAOBG
SO0973010-1 C15LADCRO
SO0973065-3 C15ABPRRO
SO0973065-3 C15CAOBG
SO0973065-3 C15LADCRO
SO0973127-1 C15ARBSIM
SO0973127-1 C15LADCRO
SO0973127-1 C15PECHRO
SO0973195-1 C15CAOBG
SO0973195-1 C15LADCRO
SO0973195-1 C15LGS5M
SO0973207-1 C15ARBSIM
SO0973207-1 C15OMB3CH
SO0973207-1 C15RPDERO
SO0973208-1 C15ABPRRO
SO0973208-1 C15NUBCA
SO0973208-1 C15OMB3CH
SO0973209-1 C15LADCRO
SO0973209-1 C15NUBCA
SO0973209-1 C15RPDERO
SO0973223-1 C15CAOBG
SO0973223-1 C15LGS5M
SO0973223-1 C15OMB3CH
SO0973251-1 C15ARBSIM
SO0973251-1 C15PV20
SO0973251-1 C15RPDERO
SO0973259-1 C15ABPRRO
SO0973259-1 C15KRLMRO
SO0973259-1 C15OMB3CH
SO0973266-1 C15CAOBG
SO0973266-1 C15MORL
SO0973266-1 C15PV20
SO0973277-1 C15EPCNWCB
SO0973277-1 C15MORL
SO0973277-1 C15NUBCA
SO0973307-2 C15CAOBG
SO0973307-2 C15OMB3CH
SO0973307-2 C15RPDERO
SO0973311-1 C15ABPRRO
SO0973311-1 C15CAOBG
SO0973311-1 C15OMB3CH
SO0973414-1 C15CAOBG
SO0973414-1 C15MORL
SO0973414-1 C15OMB3CH
SO0973457-1 C15ABPRRO
SO0973457-1 C15KRLMRO
SO0973457-1 C15LADCRO
SO0973460-1 C15DLMT
SO0973460-1 C15MORL
SO0973460-1 C15OMB3CH
SO0973466-1 C15ABPRRO
SO0973466-1 C15KRLMRO
SO0973466-1 C15LADCRO
SO0973516-1 C15DLMT
SO0973516-1 C15MORL
SO0973516-1 C15OMB3CH
SO0973541-1 C15CAOBG
SO0973541-1 C15PV20
SO0973541-1 C15RPDERO
SO0973547-1 C15CAOBG
SO0973547-1 C15MORL
SO0973547-1 C15OMB3CH
SO0973555-1 C15KRLMRO
SO0973555-1 C15LADCRO
SO0973555-1 C15MORL
SO0973561-1 C15KRLMRO
SO0973561-1 C15OMB3CH
SO0973561-1 C15RPDERO
SO0973561-2 C15KRLMRO
SO0973561-2 C15OMB3CH
SO0973561-2 C15RPDERO
SO0973561-3 C15KRLMRO
SO0973561-3 C15MORL
SO0973561-3 C15OMB3CH
SO0973561-4 C15KRLMRO
SO0973561-4 C15MORL
SO0973561-4 C15OMB3CH
SO0973739-1 C15ABPRRO
SO0973739-1 C15CAOBG
SO0973739-1 C15PV20
SO0974027-2 C15CAINM550
SO0974027-2 C15CAOBG
SO0974027-2 C15LGS5M
SO0974027-3 C15CAINM550
SO0974027-3 C15CAOBG
SO0974027-3 C15LGS5M
SO0974093-1 C15CAOBG
SO0974093-1 C15LGS5M
SO0974093-1 C15OMB3CH
SO0974104-1 C15CAOBG
SO0974104-1 C15LADCRO
SO0974104-1 C15MORL
SO0974114-1 C15KRLMRO
SO0974114-1 C15LADCRO
SO0974114-1 C15LGS5M
SO0974233-3 C15NUBCA
SO0974233-3 C15OMB3CH
SO0974233-3 C15PECHRO
SO0974437-3 C15DLMT
SO0974437-3 C15LADCRO
SO0974437-3 C15LGS5M
SO0974466-1 C15KRLMRO
SO0974466-1 C15OMB3CH
SO0974466-1 C15RPDERO
SO0974471-1 C15DLMT
SO0974471-1 C15MORL
SO0974471-1 C15OMB3CH
SO0974538-8 C15DLMT
SO0974538-8 C15MORL
SO0974538-8 C15OMB3CH
SO0974539-1 C15CAOBG
SO0974539-1 C15EPCNWCB
SO0974539-1 C15PECHRO
SO0974558-1 C15ABPRRO
SO0974558-1 C15CAOBG
SO0974558-1 C15PV20
SO0974581-1 C15MORL
SO0974581-1 C15NUBCA
SO0974581-1 C15PV20
SO0974649-1 C15CAOBG
SO0974649-1 C15LADCRO
SO0974649-1 C15PECHRO
SO0974663-1 C15CAINM550
SO0974663-1 C15KRLMRO
SO0974663-1 C15RPDERO
SO0974702-1 C15CAOBG
SO0974702-1 C15PECHRO
SO0974702-1 C15PV20
SO0974715-1 C15DLMT
SO0974715-1 C15OMB3CH
SO0974715-1 C15RPDERO
SO0974753-1 C15DLMT
SO0974753-1 C15OMB3CH
SO0974753-1 C15RPDERO
SO0974783-1 C15KRLMRO
SO0974783-1 C15OMB3CH
SO0974783-1 C15PECHRO
SO0974783-2 C15KRLMRO
SO0974783-2 C15OMB3CH
SO0974783-2 C15PECHRO
SO0974923-2 C15DLMT
SO0974923-2 C15LGS5M
SO0974923-2 C15OMB3CH

1 个答案:

答案 0 :(得分:0)

这是我在引用herehere时使用的更新代码

#Main
library(Matrix)
library(arules)
library(plyr)
library(dplyr)
library(gridExtra)

#itemList <- read.csv("ItemListAll.csv", header=TRUE, sep=",")
itemList <- read.csv("ItemListFiltered.csv", header=TRUE, sep=",")

# Converting to a Matrix ####
itemList$const = TRUE

# Remove duplicates
dim(itemList)
orders <- unique(itemList)
dim(itemList)

# Need to reshape the matrix
itemList_max_prep <- reshape(data = itemList,
                           idvar = "SALESID",
                           timevar = "ITEMID",
                           direction = "wide")

# Drop the SALESID
itemList_matrix <- as.matrix(itemList_max_prep[,-1])

# Clean up the missing values to be FALSE
itemList_matrix[is.na(itemList_matrix)] <- FALSE

# Clean up names
colnames(itemList_matrix) <- gsub(x=colnames(itemList_matrix),
                               pattern="const\\.", replacement="")

itemList_trans <- as(itemList_matrix,"transactions")

inspect(itemList_trans)

basket_rules <- apriori(itemList_trans, parameter = list(sup=0.01,conf=0.5,target="rules", minlen=3));
df_basket <- as(basket_rules,"data.frame")
df_basket$support <- ceiling(df_basket$support * 100)
df_basket$confidence<- ceiling(df_basket$confidence * 100)
df_basket$lift<- round(df_basket$lift, digits = 2)
df_basket <- df_basket[rev(order(df_basket$support)),];
write.xlsx(df_basket, "Basket_Analysis.xlsx", col.names=TRUE, row.names=TRUE)
#grid.table(head(df_basket));