【问题标题】:print decision tree in text nicely / with custom control [r]在文本中很好地打印决策树/使用自定义控件 [r]
【发布时间】:2018-11-22 05:29:29
【问题描述】:

我想用文本很好地打印决策树。例如,我可以打印树对象本身:

library(rpart)

f = as.formula('Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + Species')
fit = rpart(f, data = iris, control = rpart.control(xval = 3))

fit

产量

n= 150 

node), split, n, deviance, yval
      * denotes terminal node

 1) root 150 102.1683000 5.843333  
   2) Petal.Length< 4.25 73  13.1391800 5.179452  
     4) Petal.Length< 3.4 53   6.1083020 5.005660  
       8) Sepal.Width< 3.25 20   1.0855000 4.735000 *
       9) Sepal.Width>=3.25 33   2.6696970 5.169697 *
... # omitted

partykit 打印更整洁:

library(partykit)

as.party(fit)

产量

Model formula:
Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + Species

Fitted party:
[1] root
|   [2] Petal.Length < 4.25
|   |   [3] Petal.Length < 3.4
|   |   |   [4] Sepal.Width < 3.25: 4.735 (n = 20, err = 1.1)
|   |   |   [5] Sepal.Width >= 3.25: 5.170 (n = 33, err = 2.7)
|   |   [6] Petal.Length >= 3.4: 5.640 (n = 20, err = 1.2)
...# omitted

Number of inner nodes:    6
Number of terminal nodes: 7

有没有办法让我拥有更多控制权?例如,我不想打印nerr,或者想要标准差而不是打印err

【问题讨论】:

    标签: r decision-tree rpart party


    【解决方案1】:

    不是一个非常优雅的答案,但如果您只是想摆脱n=err=,您可以捕获输出并对其进行编辑。

    CO = capture.output(print(as.party(fit)))
    CO2 = sub("\\(.*\\)", "", CO)
    cat(paste(CO2, collapse="\n"))
    
    Model formula:
    Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + Species
    
    Fitted party:
    [1] root
    |   [2] Petal.Length < 4.25
    |   |   [3] Petal.Length < 3.4
    |   |   |   [4] Sepal.Width < 3.25: 4.735 
    |   |   |   [5] Sepal.Width >= 3.25: 5.170 
    |   |   [6] Petal.Length >= 3.4: 5.640 
    |   [7] Petal.Length >= 4.25
    

    我不确定您要插入什么标准差,但我希望您可以用相同的方式对其进行编辑。

    【讨论】:

      【解决方案2】:

      party 对象的print() 方法非常灵活,可以通过各种面板功能和自定义进行控制。有关概述,请参阅?print.party。不过,文档有些简短且技术性很强。

      在您的情况下,最简单的解决方案是设置响应 y、案例权重 w(在您的案例中默认为全 1)和所需数量的 digits 的函数:

      myfun <- function(y, w, digits = 2) {
        n <- sum(w)
        m <- weighted.mean(y, w)
        s <- sqrt(weighted.mean((y - m)^2, w) * n/(n - 1))
        sprintf("%s (serr = %s)",
          round(m, digits = digits),
          round(s, digits = digits))
      }
      

      然后您可以将其传递给您的print() 电话:

      p <- as.party(fit)
      print(p, FUN = myfun)
      ## Model formula:
      ## Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + Species
      ## 
      ## Fitted party:
      ## [1] root
      ## |   [2] Petal.Length < 4.25
      ## |   |   [3] Petal.Length < 3.4
      ## |   |   |   [4] Sepal.Width < 3.25: 4.735 (serr = 0.239)
      ## |   |   |   [5] Sepal.Width >= 3.25: 5.17 (serr = 0.289) 
      ## |   |   [6] Petal.Length >= 3.4: 5.64 (serr = 0.25)  
      ## |   [7] Petal.Length >= 4.25
      ## |   |   [8] Petal.Length < 6.05
      ## |   |   |   [9] Petal.Length < 5.15
      ## |   |   |   |   [10] Sepal.Width < 3.05: 6.055 (serr = 0.404)
      ## |   |   |   |   [11] Sepal.Width >= 3.05: 6.53 (serr = 0.38)  
      ## |   |   |   [12] Petal.Length >= 5.15: 6.604 (serr = 0.302)
      ## |   |   [13] Petal.Length >= 6.05: 7.578 (serr = 0.228)
      ## 
      ## Number of inner nodes:    6
      ## Number of terminal nodes: 7
      

      【讨论】:

        猜你喜欢
        • 2012-09-15
        • 2010-10-11
        • 1970-01-01
        • 2017-12-31
        • 2019-01-26
        • 2017-02-11
        • 2018-07-24
        • 1970-01-01
        • 2020-04-08
        相关资源
        最近更新 更多