gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:40:22 25 4
gpt4 key购买 nike

我的目标是使用 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

最佳答案

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

话虽如此,请尝试使用 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 - 将 R 中规则生成的规则应用于新事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40833925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com