I've created a regression model on my data using random forests in R. The output is quite large, I'm wondering if there's any way to reduce this to only the necessary pieces to make a prediction?

The training data set contains 20 variables and ~45,000 rows, which is also large. My code is listed below.

data <- readRDS("data.Rds")


train <- data[ which(set == "train")]
test <- data[ which(set == "test")]

x <- data.table(train[, 2:21, with=FALSE])
y <- as.vector(as.matrix(train[, 23, with=FALSE]))

cl <- makeCluster(detectCores())
registerDoParallel(cl, cores=4)
time <- system.time({rf.fit <- foreach(ntree=rep(500, 6),
                               .packages="randomForest") %dopar% 
                   {randomForest(x, y, ntree=ntree)}})

saveRDS(rf.fit, "rf.fit.Rds")

The output of this is ~230 MB. Once I have the model, is it possible to reduce the size to make it easier to work with? My goals with this are to identify the important variables, and make a prediction on new data.

  • $\begingroup$ What is your outcome variable of interest? Is it dichotomous, discrete, or continuous? $\endgroup$ – Matt Reichenbach Jun 9 '14 at 14:40
  • $\begingroup$ Are you working on windows? If you are on, for example, linux; the paralellized version is much more memory efficient: r-bloggers.com/… $\endgroup$ – JEquihua Jun 9 '14 at 17:37
  • $\begingroup$ My output for this round is dichotomous (number of sales). A future iteration will be continuous (revenue). My system is running Windows. I'll take a look at parallelRandomForest, is it possible to get that to run on Windows? $\endgroup$ – Lenwood Jun 9 '14 at 23:37
  • 1
    $\begingroup$ I've installed parallelRandomForest on my system. It does run MUCH faster, but it doesn't solve this problem. The output file is still over 200 MB. Is it possible to reduce the size of the randomForest model and still be able to use it to make a prediction? $\endgroup$ – Lenwood Jun 19 '14 at 16:21

I used this function to reduce my default caret-output from 137 MB to 3 MB. You can still use this model for prediction with $finalModel

## Clean Model to Save Memory

## http://stats.stackexchange.com/questions/102667/reduce-random-forest-model-memory-size
stripRF <- function(cm) {
  cm$finalModel$predicted <- NULL 
  cm$finalModel$oob.times <- NULL 
  cm$finalModel$y <- NULL
  cm$finalModel$votes <- NULL
  cm$control$indexOut <- NULL
  cm$control$index    <- NULL
  cm$trainingData <- NULL

  attr(cm$terms,".Environment") <- c()
  attr(cm$formula,".Environment") <- c()

| cite | improve this answer | |
  • $\begingroup$ that is nice. what was the train method in your case? I looked at object.size() of the $finalModel produced by method="ranger" and the forest itself is 65% of the total object size (i.e. not reducible beyond that). $\endgroup$ – coulminer Mar 17 '17 at 10:16
  • $\begingroup$ with the method = "rf", the forest was at 69% of the total object size $\endgroup$ – coulminer Mar 20 '17 at 9:29
  • $\begingroup$ version of this in python? $\endgroup$ – Brendan Cody-Kenny Oct 24 '19 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.