将 R 中的规则生成的规则应用于新事务
Applying rules generated from arules in R to new transactions
我的目标是使用 R 包 arules
生成的规则来预测每个交易的 topic
(每个交易有 1 个主题),其中每个交易是一个单词的集合文档。我有一个训练集trans.train
(用来创建规则)和测试集trans.test
(我想预测其中的"topic")。我也希望能够测试这些预测(规则右侧是正确主题的次数百分比)。
我能够确保每条规则的右侧是一个主题(如 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
我怀疑单词的关联规则和简单的置信度度量是否适合预测文档主题。
也就是说,尝试使用 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 包 arulesCBA 支持此类功能,如果您将来再次需要它。
在当前的开发版本中,arulesCBA 有一个名为 CBA_ruleset 的函数,它接受一组排序的规则和 returns 一个 CBA 分类器对象。
我的目标是使用 R 包 arules
生成的规则来预测每个交易的 topic
(每个交易有 1 个主题),其中每个交易是一个单词的集合文档。我有一个训练集trans.train
(用来创建规则)和测试集trans.test
(我想预测其中的"topic")。我也希望能够测试这些预测(规则右侧是正确主题的次数百分比)。
我能够确保每条规则的右侧是一个主题(如 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
我怀疑单词的关联规则和简单的置信度度量是否适合预测文档主题。
也就是说,尝试使用 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 包 arulesCBA 支持此类功能,如果您将来再次需要它。
在当前的开发版本中,arulesCBA 有一个名为 CBA_ruleset 的函数,它接受一组排序的规则和 returns 一个 CBA 分类器对象。