【问题标题】:how to get all terminal nodes - weight & response prediction 'ctree' in r如何获取所有终端节点 - r 中的权重和响应预测“ctree”
【发布时间】:2012-12-07 05:18:26
【问题描述】:

这是我可以用来列出所有终端节点权重的方法:但是如何添加一些代码来获得响应预测以及每个终端节点 ID 的权重:

说我希望我的输出看起来像这样

-- 下面是我到目前为止得到的重量

nodes(airct, unique(where(airct))) 

谢谢

【问题讨论】:

    标签: r data-mining decision-tree


    【解决方案1】:

    二叉树是一个很大的 S4 对象,所以有时很难提取数据。

    但是 BinaryTree 对象的 plot 方法有一个可选的面板函数,形式为 function(node) 绘制终端节点。因此,当您绘图时,您可以获得节点信息。

    这里我使用绘图函数来提取信息,甚至更好的是我使用gridExtra 包将终端节点转换为表格。

    library(party)
    library(gridExtra)
    set.seed(100)
    lls <- data.frame(N = gl(3, 50, labels = c("A", "B", "C")), 
                      a = rnorm(150) + rep(c(1, 0,150)),
                      b = runif(150))
    pond= sample(1:5,150,replace=TRUE)
    tt <- ctree(formula=N~a+b, data=lls,weights = pond)
    output.df <- data.frame()
    innerWeights <- function(node){
    
     dat <- data.frame (x=node$nodeID,
                        y=sum(node$weights),
                        z=paste(round(node$prediction,2),collapse='  '))
      grid.table(dat,
                 cols = c('ID','Weights','Prediction'),
                 h.even.alpha=1, 
                 h.odd.alpha=1,  
                 v.even.alpha=0.5, 
                 v.odd.alpha=1)
       output.df <<- rbind(output.df,dat)  # note the use of <<-
    
    }
    
    plot(tt, type='simple', terminal_panel = innerWeights)
    
    
    data
      ID Weights       Prediction
    1  4      24  0.42  0.5  0.08
    2  5      17 0.06  0.24  0.71
    3  6      24    0.08  0  0.92
    4  7     388 0.37  0.37  0.26
    

    【讨论】:

      【解决方案2】:

      这是我发现的,它可以很好地使用一些额外的信息。但我只是想把它贴在这里,以防将来有人需要它们。

      y <- do.call(rbind, nodes(tt, unique(where(tt))))
      write.table(y, 'clipboard', sep='\t') 
      

      @agstudy,让我知道你的想法。

      【讨论】:

        猜你喜欢
        • 2016-02-09
        • 2016-08-12
        • 2015-07-12
        • 2014-02-21
        • 2015-08-19
        • 2015-09-09
        • 1970-01-01
        • 2020-12-26
        • 2012-07-12
        相关资源
        最近更新 更多