Skip to content

Instantly share code, notes, and snippets.

@DexGroves
Last active December 27, 2016 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DexGroves/d6c055addf870b30d678862cc0fa8a88 to your computer and use it in GitHub Desktop.
Save DexGroves/d6c055addf870b30d678862cc0fa8a88 to your computer and use it in GitHub Desktop.
Predict with a subset of RF trees
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