【问题标题】:Get id/name of rpart model nodes获取 rpart 模型节点的 ID/名称
【发布时间】:2013-07-09 23:34:37
【问题描述】:

如何获取每行rpart 模型的终端节点的 ID(或名称)? predict.rpart 只能返回分类树的预测类别(数字或因子)或类别概率或某种组合(使用 type="matrix")。

我想做这样的事情:

fit <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis)
plot(fit) # there are 5 terminal nodes
predict(fit, type = "node_id")   # should return IDs of terminal nodes (e.g. 1-5) (does not work)

【问题讨论】:

    标签: r rpart


    【解决方案1】:

    partykit 包支持predict(..., type = "node"),包括样本内外。您可以简单地转换 rpart 对象来使用它:

    library("partykit")
    predict(as.party(fit), type = "node")  
    ## 9 7 9 9 3 3 3 3 3 8 8 3 9 5 3 3 3 7 3 5 3 9 8 9 9 5 9 8 3 3 3 7 7 3 7 3 5 ## 9 5 8 
    ## 9 7 9 9 3 3 3 3 3 8 8 3 9 5 3 3 3 7 3 5 3 9 8 9 9 5 9 8 3 3 3 7 7 3 7 3 5 ## 9 5 8 
    ## 9 5 9 9 3 7 3 7 9 7 8 3 9 3 3 3 5 9 5 8 9 9 9 3 3 5 3 7 5 3 7 7 3 7 3 3 7 ## 5 7 9 
    ## 9 5 9 9 3 7 3 7 9 7 8 3 9 3 3 3 5 9 5 8 9 9 9 3 3 5 3 7 5 3 7 7 3 7 3 3 7 ## 5 7 9 
    ## 5 
    ## 5 
    table(predict(as.party(fit), type = "node")) 
    ##  3  5  7  8  9 
    ## 29 12 14  7 19 
    

    【讨论】:

      【解决方案2】:

      对于该模型,有 4 个拆分,产生 5 个“终端节点”或 rpart 中使用的术语:&lt;leaf&gt;s。我不明白为什么应该有 5 个预测。预测是针对特定情况的,叶子是用于进行这些预测的可变数量的分割的结果。原始数据集中最终出现在叶子中的行数可能是您想要的,在这种情况下,这些是获取这些数字的方法:

      # Row-wise predicted class
      fit$where
      
      # counts of cases in leaves of prediction rules
      table(fit$where)
       3  5  7  8  9 
      29 12 14  7 19 
      

      为了组装适用于特定叶的labels(fit),您需要遍历规则树并累积所有用于生成特定叶的分割的所有标签。你可能想看看:

      ?print.rpart    
      ?rpart.object
      ?text.rpart
      ?labels.rpart
      

      【讨论】:

      • 谢谢,1-5我指的是终端节点的ID。您的回答有效,我可以简单地使用 kyphosis["id_node"] &lt;-fit$where 将叶子 ID 分配给原始数据框。
      【解决方案3】:

      上面使用$where的方法只弹出树框中的行号。因此,在使用kyphosis$ID = fit$where 时,可能会为一些观察结果分配节点 ID 而不是叶节点 ID 要获取实际的叶节点 ID,请使用以下命令:

      MyID <- row.names(fit$frame)
      kyphosis$ID <- MyID[fit$where]
      

      【讨论】:

        【解决方案4】:

        为了预测新数据上的叶子,可以使用 rpart.plot 包中的 rpart.predict(fit, newdata, nn = TRUE) 将节点名称添加到输出中。

        这是一个孤立的 rpart 叶子预测器:

        rpart_leaves <- function(fit, newdata, type = c("where", "leaf"), na.action = na.pass) {
          if (is.null(attr(newdata, "terms"))) {
            Terms <- delete.response(fit$terms)
            newdata <- model.frame(Terms, newdata, na.action = na.action,
                                   xlev = attr(fit, "xlevels"))
            if (!is.null(cl <- attr(Terms, "dataClasses")))
              .checkMFClasses(cl, newdata, TRUE)
          }
          newdata <- rpart:::rpart.matrix(newdata)
          where <- unname(rpart:::pred.rpart(fit, newdata))
          if (match.arg(type) == "where")
            return(where)
          rownames(fit$frame)[where]
        }
        

        【讨论】:

          猜你喜欢
          • 2021-12-06
          • 2017-02-23
          • 2012-03-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多