在插入符号中如何保存最小尺寸模型。在此示例中,gbmFit1
包含gbmFit1$trainingData
。保存gbmFit1
会保存所有此类变量。由于我的训练数据很大,我想摆脱所有这些额外的变量,并希望以最小的尺寸保存模型。
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
仅使用predict(gbmFit1$finalModel, x[1:10, ])
会出错:
predict(gbmFit1$finalModel, x[1:10, ])
##Error in paste("Using", n.trees, "trees...\n") :
##argument "n.trees" is missing, with no default
答案 0 :(得分:0)
我认为应该这样做:
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.
您可能希望在此处阅读插入符号中的trainControl
功能:https://topepo.github.io/caret/model-training-and-tuning.html#control