R先验修剪规则

时间:2018-02-21 15:24:33

标签: r apriori arules

我正在使用r中的先验(库arules)进行规则关联的数据挖掘。

我想在虚拟变量中转换因子,例如: g。,color = {red,blue,green}我将为每个实例做三个新变量(红色=是,蓝色=否,绿色=否)。

当我做先验时,我遇到冗余规则的问题,如红色=是=>绿色=否或{红色=是,绿色=否} => {一个任意不相关的东西}。

如何修改规则以避免获得这些冗余?数据集是pima indians糖尿病(一个非常着名和典型的数据集)。

library(arules)

pima<-read.csv("pima.dat",header=F,comment.char = "@")
names(pima)<-names(pima) <-c("Preg", "Plas", "Pres", "Skin", "Insu", "Mass", "Pedi", "Age", "Class")



pima$Plas<-ifelse(pima$Plas==0&pima$Class=="tested_negative",floor(mean(pima$Plas[pima$Plas!=0&pima$Class=="tested_negative"])),floor(pima$Plas))
pima$Plas<-ifelse(pima$Plas==0&pima$Class=="tested_positive",floor(mean(pima$Plas[pima$Plas!=0&pima$Class=="tested_positive"])),floor(pima$Plas))

pima$Pres<-ifelse(pima$Pres==0&pima$Class=="tested_negative",floor(mean(pima$Pres[pima$Pres!=0&pima$Class=="tested_negative"])),floor(pima$Pres))
pima$Pres<-ifelse(pima$Pres==0&pima$Class=="tested_positive",floor(mean(pima$Pres[pima$Pres!=0&pima$Class=="tested_positive"])),floor(pima$Pres))

pima$Skin<-ifelse(pima$Skin==0&pima$Class=="tested_negative",floor(mean(pima$Skin[pima$Skin!=0&pima$Class=="tested_negative"])),floor(pima$Skin))
pima$Skin<-ifelse(pima$Skin==0&pima$Class=="tested_positive",floor(mean(pima$Skin[pima$Skin!=0&pima$Class=="tested_positive"])),floor(pima$Skin))

pima$Insu<-ifelse(pima$Insu==0&pima$Class=="tested_negative",floor(mean(pima$Insu[pima$Insu!=0&pima$Class=="tested_negative"])),floor(pima$Insu))
pima$Insu<-ifelse(pima$Insu==0&pima$Class=="tested_positive",floor(mean(pima$Insu[pima$Insu!=0&pima$Class=="tested_positive"])),floor(pima$Insu))

pima$Mass<-ifelse(pima$Mass==0&pima$Class=="tested_negative",floor(mean(pima$Mass[pima$Mass!=0&pima$Class=="tested_negative"])),floor(pima$Mass))
pima$Mass<-ifelse(pima$Mass==0&pima$Class=="tested_positive",floor(mean(pima$Mass[pima$Mass!=0&pima$Class=="tested_positive"])),floor(pima$Mass))

#Discretización de las variables
pima[[ "Age"]] = ordered( cut ( pima[[ "Age"]], c(21,30,45,65,100) ) ,
                              labels = c ("Joven", "Mediana edad", "Mayor", "Viejo"))
pima[[ "Preg"]] = ordered( cut ( pima[[ "Preg"]], c(0,0.9,4,8,20),right=FALSE, include.lowest = TRUE ) ,
                          labels = c ("Ningún embarazo", "Pocos embarazos", "Bastantes embarazos", "Muchos embarazos"))
pima[[ "Pres"]] = ordered( cut ( pima[[ "Pres"]], c(0,70,92,102,110,Inf),right=FALSE, include.lowest = TRUE ) ,
                           labels = c ("hipotensión", "tensión normal", "hipertensión leve", "hipertensión media-grave", "hipertensión grave"))
pima[[ "Insu"]] = ordered( cut ( pima[[ "Insu"]], c(0,16,166,Inf),right=FALSE, include.lowest = TRUE ) ,
                           labels = c ("Insulina baja", "Insulina normal", "Insulina alta"))
pima[[ "Mass"]] = ordered( cut ( pima[[ "Mass"]], c(0,18.5,25,30,Inf),right=FALSE, include.lowest = TRUE ) ,
                           labels = c ("Bajo peso", "Peso normal", "Sobrepeso","Obesidad"))
pima[[ "Plas"]] = ordered( cut ( pima[[ "Plas"]], c(0,140,192,Inf),right=FALSE, include.lowest = TRUE ) ,
                           labels = c ("Glucosa normal", "Glucosa alta", "Glucosa muy alta"))
pima[[ "Pedi"]] = ordered( cut ( pima[[ "Pedi"]], c(0,0.37,0.6,Inf),right=FALSE, include.lowest = TRUE ) ,
                           labels = c ("Función pedigree baja", "Función pedigree normal", "Función pedigree alta"))
pima[[ "Skin"]] = ordered( cut ( pima[[ "Skin"]], c(0,15,40,Inf),right=FALSE, include.lowest = TRUE ) ,
                           labels = c ("Bajo espesor de piel", "Espesor de piel normal", "Espesor de piel alto"))
pima$Class = factor(ifelse(pima$Class=="tested_positive","Diabético","No diabético"))


library(psych)
new <- dummy.code(pima_df$Preg)
pima_df <- data.frame(new,pima_df)


pima_df<-pima_df[,-5]


pima_df[,1] <- factor(ifelse(pima_df[,1]==0, 'No', 'Sí'))
pima_df[,2] <- factor(ifelse(pima_df[,2]==0, 'No', 'Sí'))
pima_df[,3] <- factor(ifelse(pima_df[,3]==0, 'No', 'Sí'))
pima_df[,4] <- factor(ifelse(pima_df[,4]==0, 'No', 'Sí'))




pima2 <- as(pima_df, "transactions")



rules_negadas <- apriori(pima2, parameter = list(support = 0.1, confidence = 0.8, minlen = 2))
summary(rules_negadas)
inspect(head(rules_negadas))
quality(head(rules_negadas))
#Ordenar las reglas por el campo que más nos interese por ejemplo confianza
rulesSorted_negadas = sort(rules_negadas, by = "confidence")
inspect(head(rulesSorted_negadas,20))

1 个答案:

答案 0 :(得分:1)

不要创建虚拟变量。将颜色作为一个因素,转换为交易将创建相应的项目(color=redcolor=bluecolor=green)。

如果您已经有虚拟变量,但您不希望负值成为项目,那么将它们转换为逻辑。示例:pima_df[,1] <- pima_df[,1] == 'Si'

请查看?transactionshttps://rawgit.com/mhahsler/Introduction_to_Data_Mining_R_Examples/master/chap6.html#create-transactions中的示例,详细了解相关信息。

如果您想要green=yesred=no,而不是在同一规则中,则您遇到麻烦,因为这不是您可以在关联规则挖掘中轻松实施的。