有没有办法获得R中随机森林树中用于分类的实例? [英] Is there a way to get the instances used for classification in random forest tree in R?
问题描述
R中randomForest包中的getTree
函数显示随机森林中使用的特定树的结构.
The getTree
function in randomForest package in R displays the structure of the a particular tree used in the random forest.
这是虹膜数据集上的一个例子
Here is an example on the iris dataset
library(randomForest)
data(iris)
rf <- randomForest(Species ~ ., iris)
getTree(rf, 1)
这显示了500号树1的输出:
This shows the output of tree #1 of 500:
left daughter right daughter split var split point status prediction
1 2 3 3 2.50 1 0
2 0 0 0 0.00 -1 1
3 4 5 4 1.65 1 0
4 6 7 4 1.35 1 0
5 8 9 3 4.85 1 0
6 0 0 0 0.00 -1 2
7 10 11 2 3.10 1 0
8 12 13 4 1.55 1 0
9 0 0 0 0.00 -1 3
10 0 0 0 0.00 -1 3
11 0 0 0 0.00 -1 2
12 14 15 2 2.55 1 0
13 0 0 0 0.00 -1 2
14 16 17 2 2.35 1 0
15 0 0 0 0.00 -1 3
16 0 0 0 0.00 -1 3
17 0 0 0 0.00 -1 2
叶子是具有0个叶子节点和0个右子节点的节点.
The leaves are the nodes with 0 leaf daughter and 0 right daughter.
有没有办法获取那些叶子中的哪些实例(虹膜数据集的行)?
像节点2一样,它是一片叶子,其虹膜数据集的实例2、3、4都被分类为1.
Is there a way I can get which instances (rows of the iris data set) are in those leaves?
Like Node 2 which is a leaf has instance 2,3,4 from iris dataset all classified as 1.
任何帮助将不胜感激.
推荐答案
基于此答案:
可能有一个我不知道的访问功能,但是以下手动方法似乎有效:
There may be an access function I'm not aware of, but the following manual approach seems to work:
rf <- randomForest(Species ~ ., iris, keep.forest=TRUE)
pr_nodes <- attr(predict(rf, iris, nodes=TRUE),'nodes')
pr_nodes[,1] # array of 150 node assignments for 1st of 500 trees
这篇关于有没有办法获得R中随机森林树中用于分类的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!