R 中的关联规则 - 删除冗余规则 (arules)

Association rule in R - removing redundant rule (arules)

假设我们有 3 条规则:

[1] {A,B,D} -> {C}

[2] {A,B} -> {C}

[3] Whatever it is

规则[2]是规则[1]的子集(因为规则[1]包含规则[2]中的所有项目),所以规则[1]应该被淘汰(因为规则 [1] 过于具体,其信息包含在规则 [2] 中)

我在网上搜索了一下,大家都在用这些代码来去除多余的规则:

subset.matrix <- is.subset(rules.sorted, rules.sorted)
subset.matrix[lower.tri(subset.matrix, diag=T)] <- NA
redundant <- colSums(subset.matrix, na.rm=T) >= 1
which(redundant)
rules.pruned <- rules.sorted[!redundant]

我不明白代码是如何工作的。

代码第2行后,subset.matrix会变成:

      [,1] [,2] [,3]
[1,]   NA    1    0
[2,]   NA   NA    0
[3,]   NA   NA   NA

下方三角形中的单元格设置为 NA,因为规则 [2] 是规则 [1] 的子集,相应的单元格设置为 1。所以我有 2 个问题:

  1. 为什么要把下三角设为NA?如果我们这样做那么我们如何检查规则 [2] 是否是规则 [3] 的子集? (单元格已设置为NA)

  2. 在我们的例子中,规则[1]应该被删除,但是这些代码删除规则[2]而不是规则[1]。 (因为第2列的第一个单元格是1,而根据代码第3行,第2列的列和>=1,因此会被视为冗余)

如有任何帮助,我们将不胜感激!!

为了使您的代码正常工作,您需要一个兴趣度量(置信度或提升度)并且 rules.sorted 需要按置信度或提升度排序。无论如何,代码效率低得可怕,因为 is.subset() 创建了一个大小为 n^2 的矩阵,其中 n 是规则的数量。此外,is.subset for rules 合并了不正确的规则的 rhs 和 lhs。所以不用太担心实现细节。

一种更有效的方法现在作为函数 is.redundant() 在包 arules 中实现(在版本 1.4-2 中可用)。 此解释来自手册页:

A rule is redundant if a more general rules with the same or a higher confidence exists. That is, a more specific rule is redundant if it is only equally or even less predictive than a more general rule. A rule is more general if it has the same RHS but one or more items removed from the LHS. Formally, a rule X -> Y is redundant if

for some X' subset X, conf(X' -> Y) >= conf(X -> Y).

This is equivalent to a negative or zero improvement as defined by Bayardo et al. (2000). In this implementation other measures than confidence, e.g. improvement of lift, can be used as well.

查看 ? is.redundant 中的示例。

使用 arules 包删除冗余规则...

运行先验算法:

rules <- apriori(transDat, parameter = list(supp = 0.01, conf = 0.5, target = "rules", maxlen = 3))

删除冗余:

rules <- rules[!is.redundant(rules)]

检查:

arules::inspect(rules)

创建数据框:

df = data.frame(
lhs = labels(lhs(rules)),
rhs = labels(rhs(rules)), 
rules@quality)

只需查看 rstudio 中 is.redundant() 的帮助,它清楚地指出

假设有一个

规则 1 X->Y 有信心 cf1

rule2 X' -> Y with confidence cf2 其中 X' 是 X

的子集

如果规则 2 的置信度高于规则 1,即 cf2 > cf1(其中 X' 是 X 的子集),则规则 1 被认为是冗余的

也就是说,如果有一个规则,其中 lhs 的子集可以给 rhs 更有信心,那么先前的规则被认为是冗余规则。

  1. 我们将下三角设为 na,这样规则就不会成为其自身的子集

  2. 资料不足,仅靠子集化不能说规则冗余,还得考虑置信度