问题
Background:
I can make a random Forest in R:
set.seed(1)
library(randomForest)
data(iris)
model.rf <- randomForest(Species ~ ., data=iris, importance=TRUE, ntree=20, mtry = 2)
I can predict values using the randomForest object that I just made:
my_pred <- predict(model.rf)
plot(iris$Species,my_pred)
I can then peel off some random tree from the forest:
idx <- sample(x = 1:20,size = 1,replace = F)
single_tree <- getTree(model.rf,k=1)
Questions:
- How do I predict from a single tree pulled from the forest?
- Is there a different library that I should be using? (cforest, party, h2o,...)
Where I have looked so far:
- I tried the classic randomForest but there is no "unget" or "predict on get". There is "grow" but it makes a new random forest using dice, not using particular tree/s. There is "combine" but it works on randomForest objects, not what is returned from "getTree".
- I tried packing multiple trees into a single object, but it didn't work - my understanding of data to sew these together has room to improve.
- I tried looking at codes for party/cforest, but while it is allegedly made with ctree there was no "getTree" in the documentation.
- I tried a number of google searches, but didn't find anything about this particular task.
I also found generally related questions where (afaict) the answers do not answer my question:
- https://stats.stackexchange.com/questions/21152/obtaining-knowledge-from-a-random-forest
- http://tjo.hatenablog.com/entry/2014/03/10/190000 (no I don't read the kanji, but I can read the R)
- http://grokbase.com/t/r/r-help/11a5wgv1xn/r-party-extract-binarytree-from-cforest
There seems to be a fair bit about ensemble statistics, and about plotting the form of a particular tree in the forest. There does not seem to be about handling a tree in the forest.
来源:https://stackoverflow.com/questions/40875489/r-randomforest-how-to-predict-with-a-gettree-tree