【问题标题】:R how to get association rules (LHS, RHS, support, confidence, lift) from recommenderlab object?R如何从推荐实验室对象中获取关联规则(LHS,RHS,支持,置信度,提升)?
【发布时间】:2019-04-11 11:00:52
【问题描述】:

我目前正在使用R推荐器实验室构建产品推荐,在计算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"), ...)  

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

  • 获取包含列(LHS、RHS、支持、置信度、提升、计数)的关联规则数据框

【问题讨论】:

    标签: r arules recommender-systems recommenderlab


    【解决方案1】:

    你可以使用

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

    【讨论】:

      猜你喜欢
      • 2018-11-27
      • 1970-01-01
      • 2017-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-19
      相关资源
      最近更新 更多