将R中arules生成的规则应用于新事务

时间:2016-11-27 21:09:38

标签: r prediction arules

我的目标是使用R包arules生成的规则来预测每个事务的topic(每个事务有1个主题),其中每个事务都是文档中的单词集。我有一套训练集trans.train(用于创建规则)和测试集trans.test(我想预测其中的“主题”)。我也希望能够测试这些预测(规则右侧的正确主题的百分比)。

我能够确保每个规则的右侧是一个主题(如topic = earn),左侧是文档中的任何其他单词。所以我的所有规则都有以下形式:

{word1,...,wordN} -> {topic=topic1}

我已对规则进行了排序,并希望将它们应用于trans.test,以便具有最高置信度的规则预测右侧,但我无法根据文档弄清楚如何执行此操作。

对于我如何实现这一点有什么想法吗?我见过arulesCBA包,但它实现了一个更复杂的算法,而我只想使用最高置信度规则作为topic的预测器。

生成交易的代码:

library(arules)
#load data into R
filename = "C:/Users/sterl_000/Desktop/lab2file.csv"
data = read.csv(filename,header=TRUE,sep="\t")
#Get the number of columns in the matrix
col = dim(data)[2]
#Turn into logical matrix
data[,2:col]=(data[,2:col]>0)

#define % of training and test set
train_pct = 0.8
bound <- floor((nrow(data)*train_pct))    
#randomly permute rows
data <- data[sample(nrow(data)), ]   
#get training data    
data.train <- data[1:bound, ]
#get test data             
data.test <- data[(bound+1):nrow(data),]

#Turn into transaction format
trans.train = as(data.train,"transactions")
trans.test = as(data.test,"transactions")
#Create list of unique topics in 'topic=earn' format
#Allows us to specify only the topic label as the right hand side
uni_topics = paste0('topic=',unique(data[,1]))

#Get assocation rules
rules = apriori(trans.train, 
    parameter=list(support = 0.02,target= "rules", confidence = 0.5), 
    appearance = list(rhs = uni_topics,default='lhs'))

#Sort association rules by confidence
rules = sort(rules,by="confidence")

#Predict the right hand side, topic= in trans.train based on the sorted rules

示例交易:

> inspect(trans.train[3])

    items          transactionID
[1] {topic=coffee,              
     current,                   
     meet,                      
     group,                     
     statement,                 
     quota,                     
     organ,                     
     brazil,                    
     import,                    
     around,                    
     five,                      
     intern,                    
     produc,                    
     coffe,                     
     institut,                  
     reduc,                     
     intent,                    
     consid}                8760 

示例规则:

> inspect(rules[1])
    lhs       rhs          support    confidence lift    
[1] {qtli} => {topic=earn} 0.03761135 1          2.871171

2 个答案:

答案 0 :(得分:2)

我怀疑单词的关联规则和简单的置信度量是预测文档主题的理想选择。

话虽如此,请尝试使用is.subset函数。我无法在没有.csv文件的情况下重现您的示例,但以下代码应基于最高置信度为您提供trans.train[3]的预测主题。

# sort rules by conf (you already did that but for the sake of completeness)
rules<-sort(rules, decreasing=TRUE, by="confidence")

# find all rules whose lhs matches the training example
rulesMatch <- is.subset(rules@lhs,trans.train[3])

# subset all applicable rules
applicable <- rules[rulesMatch==TRUE]

# the first rule has the highest confidence since they are sorted
prediction <- applicable[1]
inspect(prediction@rhs)

答案 1 :(得分:1)

在即将发布的版本中,R包arulesCBA支持这种类型的功能,如果您将来再次需要它。

在当前的开发版本中,arulesCBA有一个名为CBA_ruleset的功能,它接受一组有序的规则并返回一个CBA分类对象。