如何设置apriori功能的前提?我想使用apriori规则为任何给定的先行者推荐产品。
您可以生成忽略前提的规则,然后使用subset
函数选择与您要查找的规则相匹配的规则。
## First generate rules
library(arules)
data(Groceries)
rules <- apriori(Groceries, parameter = list(supp = 0.001, conf = 0.8))
length(rules)
[1] 410
我不确定您是否要准确指定前提或者是否只需要包含某些项目,但您可以获得其中任何一项。假设顾客买了酸奶和米饭。他们还买了什么?
## antecedent contains the items
## irrelevant part of output removed
inspect(subset(rules, subset = lhs %ain% c("yogurt", "rice")))
lhs rhs
[1] {yogurt,rice} => {other vegetables}
[2] {root vegetables,yogurt,rice} => {other vegetables}
[3] {root vegetables,yogurt,rice} => {whole milk}
[4] {whole milk,yogurt,rice} => {other vegetables}
[5] {root vegetables,other vegetables,yogurt,rice} => {whole milk}
[6] {root vegetables,whole milk,yogurt,rice} => {other vegetables}
[7] {other vegetables,whole milk,yogurt,rice} => {root vegetables}
## antecedent is completely specified
## irrelevant part of output removed
inspect(subset(rules, subset = lhs %oin% c("yogurt", "rice")))
[1] {yogurt,rice} => {other vegetables}
使用子集,您可以限制lhs(先行词),rhs(结果)或完整项目集中的内容。您可以在subset
的帮助页面上获得更多详细信息。