caret saving minimum size model

In caret how to save minimum size model. In this example the gbmFit1 contains gbmFit1$trainingData . Saving gbmFit1 saves all such variables. As my training data is big, I want to get rid off all such extra variables and want to save the model with minimum size.

  library(mlbench)
  library(caret)
  data(Sonar)
  x <- Sonar[, colnames(Sonar)!="Class"]
  y <- Sonar$Class
  gbmFit1 <- train(x,y, method = "gbm", verbose = FALSE)
  predict(gbmFit1, x[1:10, ]) #predict for 10 samples
  ##[1] R R R R R R R R R R
  ##Levels: M R
  dim(gbmFit1$trainingData) 
  #[1] 208  61

Using only predict(gbmFit1$finalModel, x[1:10, ]) gives error:

predict(gbmFit1$finalModel, x[1:10, ])
##Error in paste("Using", n.trees, "trees...n") : 
##argument "n.trees" is missing, with no default

I think this should do it:

library(mlbench)
library(caret)
  data(Sonar)
  x <- Sonar[, colnames(Sonar)!="Class"]
  y <- Sonar$Class
tc1 <- trainControl(returnData = F)  # tells caret not to save training data.
  gbmFit1 <- train(x,y, method = "gbm", verbose = FALSE, trControl = tc1)
  predict(gbmFit1$finalModel, x[1:10, ], gbmFit1$finalModel$tuneValue$n.trees) # passes n.trees value to gbm.

You might want to read up on the trainControl functionality in caret here: https://topepo.github.io/caret/model-training-and-tuning.html#control

链接地址: http://www.djcxy.com/p/38394.html

上一篇: 如何在插入符中再现$ train和'train'对象的$ resample和$ result?

下一篇: 插入符号保存最小尺寸模型