关联规则挖掘用于同一数据源的多个切割

时间:2015-10-15 20:29:16

标签: r lapply apriori arules

目标:根据每个部门的报告生成前5个关联规则列表(按置信度)。

我现有的语法和测试数据:

# Create fake data; 1= used report, 0 = didn't use report
data <- data.frame(Dept=c('A','A','A','B','B','B'), 
              Rep1=c(1,1,1,1,1,1), 
              Rep2=c(0,0,0,1,1,1), 
              Rep3=c(1,1,1,0,0,0),
              Rep4=c(0,1,0,1,1,0),
              Rep5=c(0,0,0,0,0,0),
              Rep6=c(1,1,0,0,1,0),
              Rep7=c(1,1,1,1,1,0),
              Rep8=c(0,0,0,1,1,0),
              Rep9=c(1,0,0,1,1,0),
              Rep10=c(1,1,0,0,1,1)
              )

# Turn all variables to factors
data<-data.frame(lapply(data, factor))

# Changes 0s to NAs, only interested in rules where the report was used
data[data==0]<-NA

# lapply command to run apriori on the data when split by Dept
rules <- lapply(split(data, list(data$Dept)), function(x) {
  # Turn split data into transactions
  temp <- as(x[ , 2:length(x)], "transactions")
  # Create rules; artificially low parameters for testing
  temp <- apriori(temp, parameter = list(support=0.01, confidence=0.1,  minlen=2, maxlen=2))
  # Order rules by confidence, eventually will select top 5 (I'm able to do that), and change it to a data frame for later use
  temp <- as(sort(temp, by = "confidence")[0:length(temp)], "data.frame")
})  

# Breaks out the results into separate data.frames
list2env(rules,.GlobalEnv)

这导致每个部门约50条规则。但是,他们处于全球一级的部门。例如,Dept A data.frame有......

rules                support   confidence  lift
{Rep9=1}=>{Rep6=1}  .3333333   1.00000000  1.5
{Rep4=1}=>{Rep6=1}  .3333333   1.00000000  1.5
...    

理想情况下,我的data.frames应该看起来像......

系。 A仅报告9 data.frame

rules                support   confidence  lift
{Rep9=1}=>{Rep6=1}  .3333333   1.00000000  1.5
{Rep9=1}=>{Rep10=1}  .3333333   1.00000000  1.5    
...

系。 A仅报告4 data.frame

rules                support   confidence  lift
{Rep4=1}=>{Rep6=1}  .3333333   1.00000000  1.5
{Rep4=1}=>{Rep10=1}  .3333333   1.00000000  1.5    
...

1 个答案:

答案 0 :(得分:0)

您需要查看规则模板,以将规则的左侧(LHS)限制到某个部门。请查看arules中if(next->pid != my_worker_pid) schedule_work(next); 中的示例。

将LHS限制在某个部门的代码看起来有点像这样:

? APappearance