Cba最强外援:将R中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
3

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

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

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

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

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(360)
Cod17平台:为Blackberry生成COD文件
上一篇
安卓手机怎么玩ios游戏:安卓手机怎么连接远程机:(remote access your android phone)
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(18条)