我目前正在使用 R 包“partykit”中的 ctree,我想知道是否有办法获得从终端节点到根的完整路径。我希望每个叶子都有到根的完整路径,表示为包含节点 ID 的向量。
library(partykit)
ct <- ctree(Species ~ ., data = iris)
Model formula:
Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width
Fitted party:
[1] root
| [2] Petal.Length <= 1.9: setosa (n = 50, err = 0.0%)
| [3] Petal.Length > 1.9
| | [4] Petal.Width <= 1.7
| | | [5] Petal.Length <= 4.8: versicolor (n = 46, err = 2.2%)
| | | [6] Petal.Length > 4.8: versicolor (n = 8, err = 50.0%)
| | [7] Petal.Width > 1.7: virginica (n = 46, err = 2.2%)
Number of inner nodes: 3
Number of terminal nodes: 4
这基本上是我需要的:
[[1]]
[1] 2 1
[[2]]
[1] 5 4 3 1
[[3]]
[1] 6 4 3 1
[[4]]
[1] 7 3 1
我将不胜感激任何帮助!谢谢!