【问题标题】:Association rule mining for multiple cuts of the same datasource同一数据源的多个切割的关联规则挖掘
【发布时间】:2015-10-17 03:47:59
【问题描述】:

目标:为每个报告的每个部门生成前 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 与 Report 9 only 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 与 Report 4 only data.frame

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

【问题讨论】:

    标签: r lapply apriori arules


    【解决方案1】:

    您需要查看规则模板以将规则的左侧 (LHS) 限制为特定部门。查看 arules 中? APappearance 中的示例。

    将 LHS 限制为部门的代码如下所示:

    rules_Rep9 <- apriori(temp, parameter = list(support=0.01, confidence = 0.1), appearance = list(lhs = c("Rep9=1"), default="rhs"))

    【讨论】:

    • 我正在考虑使用子集,它确实提供了我想要的额外中断。不过,理想情况下,我希望语法能够产生所有可能的中断,而不是一次运行一个(IRL,有更多的部门和报告)。有什么想法吗?
    • 您可以轻松运行上面的代码或在 for 循环中使用子集,例如 for (dept in c("A", "B", etc.)) { select subset and mine rules }
    猜你喜欢
    • 2015-04-29
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 2011-08-07
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多