使用 arules 为新数据寻找合适的规则

机器算法验证 r 数据挖掘 关联规则
2022-03-06 15:43:40

我正在使用 R(和 arules 包)来挖掘关联规则的交易。我想做的是构建规则,然后将它们应用于新数据。

例如,假设我有很多规则,其中之一是规范的{Beer=YES} -> {Diapers=YES}.

然后我有新的交易数据,其中一条记录购买了啤酒但没有购买尿布。如何识别满足 LHS 但尚未满足 RHS 的规则?

示例:

install.packages("arules")
library(arules)

data("Groceries")
**#generate Rules omitting second record**

rules <- apriori(Groceries[-2],parameter = list(supp = 0.05, conf = 0.2,target = "rules"))

生成的规则是:

> inspect(rules)
  lhs                   rhs                   support confidence     lift
1 {}                 => {whole milk}       0.25554200  0.2555420 1.000000
2 {yogurt}           => {whole milk}       0.05603010  0.4018964 1.572722
3 {whole milk}       => {yogurt}           0.05603010  0.2192598 1.572722
4 {rolls/buns}       => {whole milk}       0.05664023  0.3079049 1.204909
5 {whole milk}       => {rolls/buns}       0.05664023  0.2216474 1.204909
6 {other vegetables} => {whole milk}       0.07484238  0.3867578 1.513480
7 {whole milk}       => {other vegetables} 0.07484238  0.2928770 1.513480

第二笔交易显示该客户,因为他们有酸奶但没有全脂牛奶,可能应该发送牛奶优惠券。如何为新交易找到“规则”中的任何适用规则?

> LIST(Groceries[2])
[[1]]
[1] "tropical fruit" "yogurt"         "coffee" 
1个回答

关键是同一个包中的is.subset-function

这是代码...

basket <- Groceries[2]
# find all rules, where the lhs is a subset of the current basket
rulesMatchLHS <- is.subset(rules@lhs,basket)
# and the rhs is NOT a subset of the current basket (so that some items are left as potential recommendation)
suitableRules <-  rulesMatchLHS & !(is.subset(rules@rhs,basket))

# here they are
inspect(rules[suitableRules])

# now extract the matching rhs ...
recommendations <- strsplit(LIST(rules[suitableRules]@rhs)[[1]],split=" ")
recommendations <- lapply(recommendations,function(x){paste(x,collapse=" ")})
recommendations <- as.character(recommendations)

# ... and remove all items which are already in the basket
recommendations <- recommendations[!sapply(recommendations,function(x){basket %in% x})]

print(recommendations)

和生成的输出......

> inspect(rules[suitableRules])
  lhs         rhs            support confidence     lift
1 {}       => {whole milk} 0.2555420  0.2555420 1.000000
2 {yogurt} => {whole milk} 0.0560301  0.4018964 1.572722

> print(recommendations)
[1] "whole milk"