I'm using caret to train models over resamples and tune learning parameters, and I can interrogate the probabilities for each test, which is great. But I'm also keen to retain the model objects and use them later without retraining -- is this possible? Basically rather than just the mdl$finalModel object, I'd like the model object for each iteration of tuning.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Not really. You could write a custom method and modify the fit
function to save them out to a file. Inside the fit
function, you would know the tuning parameter value but not what resample that the model was build with.
Max
回答2:
Thanks Max. I'm using your suggestion so I'm posting my code here should anyone else want to try this. I am working out the resample later by also saving rownames(x)
.
# Copy all model structure info from existing model type
cust.mdl <- getModelInfo("rf", regex=FALSE)[[1]]
# Override fit function so that we can save the iteration
cust.mdl$fit <- function(x=x, y=y, wts=wts, param=param, lev=lev, last=last, classProbs=classProbs, ...) {
# Dont save the final pass (dont train the final model across the entire training set)
if(last == TRUE) return(NULL)
# Fit the model
fit.obj <- getModelInfo("rf", regex=FALSE)[[1]]$fit(x, y, wts, param, lev, last, classProbs, ...)
# Create an object with data to save and save it
fit.data <- list(resample=rownames(x),
mdl=fit.obj,
#x, y, wts,
param=param, lev=lev, last=last, classProbs=classProbs,
other=list(...))
# Create a string representing the tuning params
param.str <- paste(lapply(1:ncol(param), function(x) {
paste0(names(param)[x], param[1,x])
}), collapse="-")
save(fit.data, file=paste0("rf_modeliter_", sample(1000:9999,1), "_", param.str, ".RData"))
return (fit.obj)
}