如何选择apricedent for apriori功能?

问题描述 投票:0回答:1

如何设置apriori功能的前提?我想使用apriori规则为任何给定的先行者推荐产品。

r arules
1个回答
1
投票

您可以生成忽略前提的规则,然后使用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的帮助页面上获得更多详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.