我在我的数据集上实现了Apriori算法。我得到的规则是反向复制,即:
inspect(head(rules))
lhs rhs support confidence lift count
[1] {252-ON-OFF} => {L30-ATLANTIC} 0.04545455 1 22 1
[2] {L30-ATLANTIC} => {252-ON-OFF} 0.04545455 1 22 1
[3] {252-ON-OFF} => {M01-A molle biconiche} 0.04545455 1 22 1
[4] {M01-A molle biconiche} => {252-ON-OFF} 0.04545455 1 22 1
[5] {L30-ATLANTIC} => {M01-A molle biconiche} 0.04545455 1 22 1
[6] {M01-A molle biconiche} => {L30-ATLANTIC} 0.04545455 1 22 1
可以看出规则1&规则2与LHS& RHS是互换的。有没有办法从最终结果中删除这些规则?
我看过这篇文章link,但建议的解决方案不正确。 我也看过这篇文章link,我尝试了这两个解决方案:
解决方案A:
rules <- rules[!is.redundant(rules)]
但结果总是一样的:
inspect(head(rules))
lhs rhs support confidence lift count
[1] {252-ON-OFF} => {L30-ATLANTIC} 0.04545455 1 22 1
[2] {L30-ATLANTIC} => {252-ON-OFF} 0.04545455 1 22 1
[3] {252-ON-OFF} => {M01-A molle biconiche} 0.04545455 1 22 1
[4] {M01-A molle biconiche} => {252-ON-OFF} 0.04545455 1 22 1
[5] {L30-ATLANTIC} => {M01-A molle biconiche} 0.04545455 1 22 1
[6] {M01-A molle biconiche} => {L30-ATLANTIC} 0.04545455 1 22 1
解决方案B:
# find redundant rules
subset.matrix <- is.subset(rules, rules)
subset.matrix[lower.tri(subset.matrix, diag=T)]
redundant <- colSums(subset.matrix, na.rm=T) > 1
which(redundant)
rules.pruned <- rules[!redundant]
inspect(rules.pruned)
lhs rhs support confidence lift count
[1] {} => {BRC-BRC} 0.04545455 0.04545455 1 1
[2] {} => {111-WINK} 0.04545455 0.04545455 1 1
[3] {} => {305-INGRAM HIGH} 0.04545455 0.04545455 1 1
[4] {} => {952-REVERS} 0.04545455 0.04545455 1 1
[5] {} => {002-LC2} 0.09090909 0.09090909 1 2
[6] {} => {252-ON-OFF} 0.04545455 0.04545455 1 1
[7] {} => {L30-ATLANTIC} 0.04545455 0.04545455 1 1
[8] {} => {M01-A molle biconiche} 0.04545455 0.04545455 1 1
[9] {} => {678-Portovenere} 0.04545455 0.04545455 1 1
[10] {} => {251-MET T.} 0.04545455 0.04545455 1 1
[11] {} => {324-D.S.3} 0.04545455 0.04545455 1 1
[12] {} => {L04-YUME} 0.04545455 0.04545455 1 1
[13] {} => {969-Lubekka} 0.04545455 0.04545455 1 1
[14] {} => {000-FUORI LISTINO} 0.04545455 0.04545455 1 1
[15] {} => {007-LC7} 0.04545455 0.04545455 1 1
[16] {} => {341-COS} 0.04545455 0.04545455 1 1
[17] {} => {601-ROBIE 1} 0.04545455 0.04545455 1 1
[18] {} => {608-TALIESIN 2} 0.04545455 0.04545455 1 1
[19] {} => {610-ROBIE 2} 0.04545455 0.04545455 1 1
[20] {} => {615-HUSSER} 0.04545455 0.04545455 1 1
[21] {} => {831-DAKOTA} 0.04545455 0.04545455 1 1
[22] {} => {997-997} 0.27272727 0.27272727 1 6
[23] {} => {412-CAB} 0.09090909 0.09090909 1 2
[24] {} => {S01-A doghe senza movimenti} 0.09090909 0.09090909 1 2
[25] {} => {708-Genoa} 0.09090909 0.09090909 1 2
[26] {} => {998-998} 0.54545455 0.54545455 1 12
有没有人有同样的问题,知道如何解决它?谢谢你的帮助
答案 0 :(得分:0)
您可以通过强制执行此操作,将规则对象转换为data.frame,并迭代地比较LHS / RHS事务向量。以下是使用grocery.csv dataset:
的示例inspect(head(groceryrules))
# convert rules object to data.frame
trans_frame <- data.frame(lhs = labels(lhs(groceryrules)), rhs = labels(rhs(groceryrules)), groceryrules@quality)
# loop through each row of trans_frame
rem_indx <- NULL
for(i in 1:nrow(trans_frame)) {
trans_vec_a <- c(as.character(trans_frame[i,1]), as.character(trans_frame[i,2]))
# for each row evaluated, compare to every other row in trans_frame
for(k in 1:nrow(trans_frame[-i,])) {
trans_vec_b <- c(as.character(trans_frame[-i,][k,1]), as.character(trans_frame[-i,][k,2]))
if(setequal(trans_vec_a, trans_vec_b)) {
# store the index to remove
rem_indx[i] <- i
}
}
}
这为您提供了应该删除的索引向量(因为它们是重复/反转的)
duped_trans <- trans_frame[rem_indx[!is.na(rem_indx)], ]
duped_trans
我们可以看到它确定了2个重复/反转的事务。
现在我们可以保留非重复的交易:
deduped_trans <- trans_frame[-rem_indx[!is.na(rem_indx)], ]
问题当然是上面的算法效率极低。购物清单数据集只有463笔交易。对于任何合理数量的事务,您将需要对函数进行矢量化。
答案 1 :(得分:0)
问题是您的数据集,而不是算法。在结果中,您会看到许多规则的计数为1(项目组合在事务中出现一次),规则的置信度为1,“逆”。这意味着您需要更多数据并增加最低支持。
如果您仍想有效地摆脱这种“重复”规则,那么您可以执行以下操作:
> library(arules)
> data(Groceries)
> rules <- apriori(Groceries, parameter = list(support = 0.001))
> rules
set of 410 rules
> gi <- generatingItemsets(rules)
> d <- which(duplicated(gi))
> rules[-d]
set of 385 rules
代码只保留每组规则的第一条规则与完全相同的项目。