gpt4 book ai didi

r - 如何将arules apriori输出转换为R中的数据帧

转载 作者:行者123 更新时间:2023-12-04 03:19:49 25 4
gpt4 key购买 nike

我有以下数据框 - CTVU。

MMGID_5    EMAIL
2341 1@email.x
50 1@email.x
311 1@email.x
2341 2@email.x
2387 2@email.x
57 2@email.x
2329 2@email.x
2026 3@email.x
650 3@email.x
2369 3@email.x

我想将下面创建的规则转回具有两个新列的数据框,其中第一列包含置信度最高的项目,第二列包含置信度最高的项目。

library(arules)
library(arulesViz)

CTVU <- read.csv("CTVU.csv", header = TRUE)
CTVU <- unique(CTVU[ , c(2,5) ])
CTVU <- as(split(CTVU[,"MMG5_ID"], CTVU[,"EMAIL"]), "transactions")
itemFrequencyPlot(CTVU,topN=20,type="absolute")
rules <- apriori(CTVU, parameter = list(supp = 0.001, conf = 0.1))
options(digits=2)
inspect(rules[1:5])
rules<-sort(rules, by="confidence", decreasing=TRUE)
rules <- apriori(CTVU, parameter = list(supp = 0.001, conf = 0.8,maxlen=3))

rules<-apriori(data=CTVU, parameter=list(supp=0.001,conf = 0.01,minlen=2),
appearance = list(default="rhs",lhs="289"),
control = list(verbose=F))
rules<-sort(rules, decreasing=TRUE,by="confidence")
inspect(rules[1:5])

所以最后我有一个看起来像这样的数据框:

EMAIL      MMG5_rule   Confidence
1@email.x 50 0.5
2@email.x 2341 0.2
3@email.x 2026 0.6

我做了一些研究,但未能找到解决方案。有人可以帮我弄清楚如何做到这一点吗?

最佳答案

您不需要将您的 arules 输出转换为 data.frame。如果你有一个新客户有一个已购买商品的列表,你可以用 arules::subset 找到相关的关联规则:

newCustomer <- c("toothbrush", "chocolate", "gummibears")
arules::subset(aprioriResults, subset = lhs %in% newCustomer)

subset 帮助中的更多信息:

subset works on the rows/itemsets/rules of x. The expression given in subset will be evaluated using x, so the items (lhs/rhs/items) and the columns in the quality data.frame can be directly referred to by their names.

Important operators to select itemsets containing items specified by their labels are %in% (select itemsets matching any given item), %ain% (select only itemsets matching all given item) and %pin% (%in% with partial matching).

但是,在我看来,客户接下来可能购买什么的问题更像是一个需要使用序列挖掘来回答的问题。幸运的是,arulesSequences 是一个可以做到这一点的包,而且它是由同一作者编写的,因此几乎不需要额外的工作。

关于r - 如何将arules apriori输出转换为R中的数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39168315/

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