由Rh中的lhs子集化arules

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

我想通过以下方式在R中运行apriori算法生成的规则的子集。

规则子集必须具有LHS,其必须仅具有另一列表中的任何项目(例如项目)。对RHS没有任何限制。

我尝试了以下代码,但无法获得预期的结果:

> library(arules)
> library(datasets)
> data(Groceries)
> rules <- apriori(Groceries, parameter = list(supp = 0.001, conf = 0.8))
inspect(head(rules))
    lhs                                 rhs            support     confidence lift     
[1] {liquor,red/blush wine}          => {bottled beer} 0.001931876 0.9047619  11.235269
[2] {curd,cereals}                   => {whole milk}   0.001016777 0.9090909   3.557863
[3] {yogurt,cereals}                 => {whole milk}   0.001728521 0.8095238   3.168192
[4] {butter,jam}                     => {whole milk}   0.001016777 0.8333333   3.261374
[5] {soups,bottled beer}             => {whole milk}   0.001118454 0.9166667   3.587512
[6] {napkins,house keeping products} => {whole milk}   0.001321810 0.8125000   3.179840

items = c("curd","cereals")
rules.subset2 <- subset(rules, subset = all(lhs %in% items))

这个子设置操作导致以下结果(这是错误的,因为我只想在规则子集中将“curd and cereals”作为LHS)

inspect(head(rules.subset2))
          lhs                                                                           rhs                support     confidence lift     
    [1]   {liquor,red/blush wine}                                                    => {bottled beer}     0.001931876 0.9047619  11.235269
    [2]   {curd,cereals}                                                             => {whole milk}       0.001016777 0.9090909   3.557863
    [3]   {yogurt,cereals}                                                           => {whole milk}       0.001728521 0.8095238   3.168192
    [4]   {butter,jam}                                                               => {whole milk}       0.001016777 0.8333333   3.261374
    [5]   {soups,bottled beer}                                                       => {whole milk}       0.001118454 0.9166667   3.587512
    [6]   {napkins,house keeping products}                                           => {whole milk}       0.001321810 0.8125000   3.179840

我试着在这个网站上找到答案,但没有运气。我也尝试了其他各种方法,但我没有成功。

非常感谢你的任何帮助。

r subset apriori
3个回答
1
投票

当我尝试这个时它起作用了:

rules.subset2 <- subset(rules, lhs %in% c("cereals", "curd"))

同时在lhs中包含“谷物”和“凝乳”的多步骤:

sub_2<- subset(rules, lhs %in% "cereals") sub_3<- subset(sub_2, lhs %in% "curd")


0
投票

我认为运营商是%ain%,所以类似于:

lhs %oin% c('cereals', 'curd')

0
投票

文档中给出了一个示例

## select only rules with items "age=Young" and "workclass=Private" in
## the left-hand-side
rules.sub <- subset(rules, subset = lhs %ain% 
    c("age=Young", "workclass=Private"))
© www.soinside.com 2019 - 2024. All rights reserved.