Sorting rules by lift and confidence

橙三吉。 提交于 2019-11-29 15:47:37

Assuming you got

library(arules)
data("Adult")
rules <- apriori(Adult, parameter = list(supp = 0.5, conf = 0.9, target = "rules"))

then you could try

df <- as(rules, "data.frame") 
df[order(df$lift, df$confidence), ]

I have not thought about this. You can copy & paste the following code into your R session after you load arules.

setMethod("sort", signature(x = "associations"),
  function (x, decreasing = TRUE, na.last = NA, by = "support", ...) {
    q <- quality(x)
    q <- q[, pmatch(by, colnames(q)), drop = FALSE]
    if(is.null(q)) stop("Unknown interest measure to sort by.")
    if(length(x) == 0) return(x)

    x[do.call(order, c(q, list(na.last = na.last, decreasing = decreasing)))]
}) 

Now your original code should work.

> data("Adult")
> rules <- apriori(Adult, parameter = list(supp = 0.5, conf = 0.9, target = "rules"))
> inspect(head(sort(rules, by=c("supp", "conf"))))
  lhs                               rhs                   support confidence      lift
1 {}                             => {capital-loss=None} 0.9532779  0.9532779 1.0000000
2 {}                             => {capital-gain=None} 0.9173867  0.9173867 1.0000000
3 {capital-gain=None}            => {capital-loss=None} 0.8706646  0.9490705 0.9955863
4 {capital-loss=None}            => {capital-gain=None} 0.8706646  0.9133376 0.9955863
5 {native-country=United-States} => {capital-loss=None} 0.8548380  0.9525461 0.9992323
6 {native-country=United-States} => {capital-gain=None} 0.8219565  0.9159062 0.9983862

This will be part of the next release of arules.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!