我希望通过以下方式对在R中运行apiriori算法所产生的规则进行子集化。
规则子集必须具有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
我试着在这个网站上找到答案,但没有运气。我也尝试了其他各种方法,但我没有成功。
我很感激您的任何帮助。
答案 0 :(得分: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")
答案 1 :(得分:0)
我认为运算符是%ain%
,所以类似于:
lhs %oin% c('cereals', 'curd')
答案 2 :(得分: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"))