我想以以下方式在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))
此子设置操作导致以下内容(这是错误的,因为我只想在规则子集中将"凝乳和谷物"作为 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
我试图在这个网站上找到答案,但没有运气。我也尝试了各种其他方法,但没有成功。
我将不胜感激您的任何帮助。
当我尝试这个时,它有效:
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")
我认为运算符是%ain%
,所以像这样:
lhs %oin% c('cereals', 'curd')
文档给出了一个示例
## 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"))