R 如何从 recommenderlab 对象获取关联规则(左轴、右轴、支持度、置信度、提升度)?

R how to get association rules (LHS, RHS, support, confidence, lift) from recommenderlab object?

我目前正在使用 R recommenderlab 构建产品推荐,在计算 AR 推荐器之后,我希望了解关联规则,但我找不到任何原因从推荐器对象中提取完整的关联规则。

下面是示例数据集

m <- matrix(sample(c(0,1), 50, replace=TRUE), nrow=5, ncol=10,
            dimnames=list(users=paste("u", 1:5, sep=''),
                           items=paste("i", 1:10, sep='')))

将矩阵转换为 binaryRatingMatrix

b <- as(m, "binaryRatingMatrix")

使用训练数据创建基于用户的 CF 推荐器

r <- Recommender(getData(scheme, "train"), "AR")

查看 AR 推荐对象 r@model$rule_base 我发现 "rule_base"

Formal class 'Recommender' [package "recommenderlab"] with 5 slots
  ..@ method  : chr "AR"
  ..@ dataType: chr "binaryRatingMatrix"
  ..@ ntrain  : int 5
  ..@ model   :List of 9
  .. ..$ description    : chr "AR: rule base"
  .. ..$ rule_base      :Formal class 'rules' [package "arules"] with 4 slots
  .. .. .. ..@ lhs    :Formal class 'itemMatrix' [package "arules"] with 3 slots
  .. .. .. .. .. ..@ data       :Formal class 'ngCMatrix' [package "Matrix"] with 5 slots
  .. .. .. .. .. .. .. ..@ i       : int [1:145] 1 1 1 1 2 0 0 0 5 5 ...
  .. .. .. .. .. .. .. ..@ p       : int [1:80] 0 1 2 3 4 5 6 7 8 9 ...
  .. .. .. .. .. .. .. ..@ Dim     : int [1:2] 10 79
  .. .. .. .. .. .. .. ..@ Dimnames:List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. ..@ factors : list()
  .. .. .. .. .. ..@ itemInfo   :'data.frame':  10 obs. of  1 variable:
  .. .. .. .. .. .. ..$ labels: chr [1:10] "i1" "i2" "i3" "i4" ...
  .. .. .. .. .. ..@ itemsetInfo:'data.frame':  0 obs. of  0 variables
  .. .. .. ..@ rhs    :Formal class 'itemMatrix' [package "arules"] with 3 slots
  .. .. .. .. .. ..@ data       :Formal class 'ngCMatrix' [package "Matrix"] with 5 slots
  .. .. .. .. .. .. .. ..@ i       : int [1:79] 6 4 9 3 8 4 9 3 6 3 ...
  .. .. .. .. .. .. .. ..@ p       : int [1:80] 0 1 2 3 4 5 6 7 8 9 ...
  .. .. .. .. .. .. .. ..@ Dim     : int [1:2] 10 79
  .. .. .. .. .. .. .. ..@ Dimnames:List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. ..@ factors : list()
  .. .. .. .. .. ..@ itemInfo   :'data.frame':  10 obs. of  1 variable:
  .. .. .. .. .. .. ..$ labels: chr [1:10] "i1" "i2" "i3" "i4" ...
  .. .. .. .. .. ..@ itemsetInfo:'data.frame':  0 obs. of  0 variables
  .. .. .. ..@ quality:'data.frame':    79 obs. of  4 variables:
  .. .. .. .. ..$ support   : num [1:79] 0.2 0.2 0.2 0.2 0.4 0.4 0.4 0.4 0.4 0.4 ...
  .. .. .. .. ..$ confidence: num [1:79] 1 1 1 1 1 1 1 1 1 1 ...
  .. .. .. .. ..$ lift      : num [1:79] 1.67 1.25 1.25 1.25 1.67 ...
  .. .. .. .. ..$ count     : num [1:79] 1 1 1 1 2 2 2 2 2 2 ...
  .. .. .. ..@ info   :List of 4
  .. .. .. .. ..$ data         : symbol data
  .. .. .. .. ..$ ntransactions: int 5
  .. .. .. .. ..$ support      : num 0.1
  .. .. .. .. ..$ confidence   : num 0.8
  .. ..$ support        : num 0.1
  .. ..$ confidence     : num 0.8
  .. ..$ maxlen         : num 3
  .. ..$ sort_measure   : chr "confidence"
  .. ..$ sort_decreasing: logi TRUE
  .. ..$ apriori_control:List of 1
  .. .. ..$ verbose: logi FALSE
  .. ..$ verbose        : logi FALSE
  ..@ predict :function (model, newdata, n = 10, data = NULL, type = c("topNList", "ratings", "ratingMatrix"), ...)  

问题:如何从推荐对象中提取关联规则作为数据框?

你可以使用

#Convert rules into data frame
rules3 = as(rules, "data.frame")