将R Arules生成的规则应用于新交易



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

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

{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

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

话虽如此,请尝试使用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)

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

在当前开发版本中,ArulesCBA拥有一个名为CBA_RUULESET的函数量,该函数量接受一组排序的规则并返回CBA分类器对象。

相关内容

  • 没有找到相关文章

最新更新