【问题标题】:Caret - is it possible to save each models from tuning?插入符号 - 是否可以从调整中保存每个模型?
【发布时间】:2014-11-10 08:56:35
【问题描述】:

我正在使用插入符号来训练模型重采样和调整学习参数,我可以询问每个测试的概率,这很棒。但我也很想保留模型对象并在以后使用它们而无需重新训练——这可能吗?基本上,我想要的不仅仅是 mdl$finalModel 对象,而是每次调优迭代的模型对象。

【问题讨论】:

    标签: r r-caret


    【解决方案1】:

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

    最大

    【讨论】:

      【解决方案2】:

      谢谢马克斯。我正在使用您的建议,因此如果其他人想尝试此操作,我将在此处发布我的代码。我稍后会通过保存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)
      }
      

      【讨论】:

        猜你喜欢
        • 2017-03-08
        • 2018-02-07
        • 1970-01-01
        • 2015-12-17
        • 1970-01-01
        • 2015-03-15
        • 2017-08-18
        • 1970-01-01
        • 2019-03-23
        相关资源
        最近更新 更多