Caret - is it possible to save each models from tuning?

别等时光非礼了梦想. 提交于 2019-12-13 16:27:13

问题


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)
}


来源:https://stackoverflow.com/questions/26845933/caret-is-it-possible-to-save-each-models-from-tuning

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!