Caret - 可以保存每个模型免于调整吗?

时间:2014-11-10 08:56:36

标签: r r-caret

我正在使用插入符号来训练模型而不是重新采样并调整学习参数,我可以查询每个测试的概率,这很好。但是我也热衷于保留模型对象并在以后使用它们而不进行再培训 - 这可能吗?基本上而不仅仅是mdl $ finalModel对象,我希望每次迭代调整都有模型对象。

2 个答案:

答案 0 :(得分:3)

不是真的。您可以编写custom method并修改fit函数以将其保存到文件中。在fit函数内部,您将知道调整参数值,但不知道构建模型的重采样。

最高

答案 1 :(得分:2)

谢谢Max。我正在使用你的建议,所以我在这里发布我的代码,如果有人想试试这个。我稍后通过保存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)
}
相关问题