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


11

我正在使用R(和arules软件包)来挖掘交易的关联规则。我要做的是构造规则,然后将其应用于新数据。

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

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

R示例:

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" 

Answers:


19

关键是同一包中的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"

Steffen-很棒!非常感谢,我没有看到该功能。我可以看到按提升次数(或其他衡量标准)进行排名,以确定在几场比赛中要遵循哪个规则将非常容易。
B_Miner 2012年

我知道这已经很老了,但希望有人回应。如果我想直接放basket <- "tropical fruit" "yogurt" "coffee"怎么办?
HonzaB

@HonzaB,我认为您需要将其转换为正确的类型,ala:as(list(basket), "itemMatrix")
Harlan
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.