r - 从 rpart 包中的决策规则中提取信息

标签 r machine-learning classification decision-tree

我需要从决策树的规则中提取信息。我在 R 中使用 rpart 包。我使用包中的演示数据来解释我的需求:

data(stagec)
fit<- rpart(formula = pgstat ~ age + eet + g2 + grade + gleason + ploidy, data = stagec, method = "class", control=rpart.control(cp=0.05))
fit

打印合身显示

n= 146 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

 1) root 146 54 0 (0.6301370 0.3698630)  
   2) grade< 2.5 61  9 0 (0.8524590 0.1475410) *
   3) grade>=2.5 85 40 1 (0.4705882 0.5294118)  
     6) g2< 13.2 40 17 0 (0.5750000 0.4250000)  
      12) ploidy=diploid,tetraploid 31 11 0 (0.6451613 0.3548387) *
      13) ploidy=aneuploid 9  3 1 (0.3333333 0.6666667) *
     7) g2>=13.2 45 17 1 (0.3777778 0.6222222)  
      14) g2>=17.91 22  8 0 (0.6363636 0.3636364) *
      15) g2< 17.91 23  3 1 (0.1304348 0.8695652) *

例如我想获取第 12 个节点的如下信息

如果 Grade>=2.5 且 g2< 13.2 且倍性为(二倍体、四倍体),则预测 0 类的置信度为 65%。对此的任何指示都会非常有帮助。

谢谢

最佳答案

rpart.plot 软件包版本3.0(2018年7月)有一个功能 rpart.rules 用于为树生成一组规则。例如

library(rpart.plot)
data(stagec)
fit <- rpart(formula = pgstat ~ ., data = stagec, method = "class", control=rpart.control(cp=0.05))
rpart.rules(fit)

给出

pgstat                                                                   
  0.15 when grade <  3                                                   
  0.35 when grade >= 3 & g2 <  13       & ploidy is diploid or tetraploid
  0.36 when grade >= 3 & g2 >=       18                                  
  0.67 when grade >= 3 & g2 <  13       & ploidy is             aneuploid
  0.87 when grade >= 3 & g2 is 13 to 18 

还有

rpart.rules(fit, roundint=FALSE, clip.facs=TRUE)

给出

pgstat                                                           
  0.15 when grade <  2.5                                         
  0.35 when grade >= 2.5 & g2 <  13       & diploid or tetraploid
  0.36 when grade >= 2.5 & g2 >=       18                        
  0.67 when grade >= 2.5 & g2 <  13       & aneuploid
  0.87 when grade >= 2.5 & g2 is 13 to 18                        

有关更多示例,请参阅第 4 章 rpart.plot vignette .

关于r - 从 rpart 包中的决策规则中提取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36401411/

相关文章:

r - 如何解释 R 中 SVM 的预测结果?

r - 基于具有附加条件的其他列,在 R 中创建新列

r - 在 R 中仅按组保存最后一个重复项

python - Keras 自定义损失函数不返回任何内容

machine-learning - 了解逻辑回归的概率解释

java - 使用weka的命令行创建阈值文件

machine-learning - 更改成本矩阵对 weka 结果没有影响

r - 加速 gganimate 渲染

R - 坚持使用 plot() - 根据槽值着色 shapefile 多边形

machine-learning - 这个重采样示例会发生什么