Last active
December 27, 2016 12:42
-
-
Save DexGroves/d6c055addf870b30d678862cc0fa8a88 to your computer and use it in GitHub Desktop.
Predict with a subset of RF trees
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("randomForest") | |
data(mtcars) | |
rf <- randomForest(mpg ~ ., data = mtcars, ntree = 10) | |
preds <- predict(rf, newdata = mtcars, predict.all = TRUE) | |
# Aggregate predictions | |
preds$aggregate | |
# Invididual tree predictions | |
preds$individual | |
predict_rf_subset <- function(model, newdata, subset, ...) { | |
tree_p <- predict(model, newdata, predict.all = TRUE)$individual | |
apply(tree_p[, subset], 1, mean) # Mean prediction across subset trees | |
} | |
predict_rf_subset(rf, mtcars, c(1, 4, 7)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment