![](/img/trans.png)
[英]R mlr package - is it possible to save all models from Parameter tuning?
[英]Caret - is it possible to save each models from tuning?
我正在使用插入符號在重采樣上訓練模型並調整學習參數,並且可以詢問每個測試的概率,這很棒。 但是我也很想保留模型對象,以后再使用它們而無需重新培訓-這可能嗎? 基本上,而不僅僅是mdl $ finalModel對象,我希望模型對象可用於每次調整。
並不是的。 您可以編寫一個自定義方法並修改fit
函數以將其保存到文件中。 在fit
函數中,您會知道調整參數的值,但不知道該模型是用來進行重新采樣的。
馬克斯
謝謝麥克斯。 我正在使用您的建議,因此如果有人要嘗試此方法,請在此處發布我的代碼。 我稍后還會通過保存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)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.