詳解R語言caret包trainControl函數(shù)
更新時間:2022年08月08日 09:37:12 作者:嘛里嘛里哄
這篇文章主要介紹了R語言caret包trainControl函數(shù)詳解,本文通過源碼分析給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
trainControl參數(shù)詳解
源碼
caret::trainControl <- function (method = "boot", number = ifelse(grepl("cv", method), 10, 25), repeats = ifelse(grepl("[d_]cv$", method), 1, NA), p = 0.75, search = "grid", initialWindow = NULL, horizon = 1, fixedWindow = TRUE, skip = 0, verboseIter = FALSE, returnData = TRUE, returnResamp = "final", savePredictions = FALSE, classProbs = FALSE, summaryFunction = defaultSummary, selectionFunction = "best", preProcOptions = list(thresh = 0.95, ICAcomp = 3, k = 5, freqCut = 95/5, uniqueCut = 10, cutoff = 0.9), sampling = NULL, index = NULL, indexOut = NULL, indexFinal = NULL, timingSamps = 0, predictionBounds = rep(FALSE, 2), seeds = NA, adaptive = list(min = 5, alpha = 0.05, method = "gls", complete = TRUE), trim = FALSE, allowParallel = TRUE) { if (is.null(selectionFunction)) stop("null selectionFunction values not allowed") if (!(returnResamp %in% c("all", "final", "none"))) stop("incorrect value of returnResamp") if (length(predictionBounds) > 0 && length(predictionBounds) != 2) stop("'predictionBounds' should be a logical or numeric vector of length 2") if (any(names(preProcOptions) == "method")) stop("'method' cannot be specified here") if (any(names(preProcOptions) == "x")) stop("'x' cannot be specified here") if (!is.na(repeats) & !(method %in% c("repeatedcv", "adaptive_cv"))) warning("`repeats` has no meaning for this resampling method.", call. = FALSE) if (!(adaptive$method %in% c("gls", "BT"))) stop("incorrect value of adaptive$method") if (adaptive$alpha < 1e-07 | adaptive$alpha > 1) stop("incorrect value of adaptive$alpha") if (grepl("adapt", method)) { num <- if (method == "adaptive_cv") number * repeats else number if (adaptive$min >= num) stop(paste("adaptive$min should be less than", num)) if (adaptive$min <= 1) stop("adaptive$min should be greater than 1") } if (!(search %in% c("grid", "random"))) stop("`search` should be either 'grid' or 'random'") if (method == "oob" & any(names(match.call()) == "summaryFunction")) { warning("Custom summary measures cannot be computed for out-of-bag resampling. ", "This value of `summaryFunction` will be ignored.", call. = FALSE) } list(method = method, number = number, repeats = repeats, search = search, p = p, initialWindow = initialWindow, horizon = horizon, fixedWindow = fixedWindow, skip = skip, verboseIter = verboseIter, returnData = returnData, returnResamp = returnResamp, savePredictions = savePredictions, classProbs = classProbs, summaryFunction = summaryFunction, selectionFunction = selectionFunction, preProcOptions = preProcOptions, sampling = sampling, index = index, indexOut = indexOut, indexFinal = indexFinal, timingSamps = timingSamps, predictionBounds = predictionBounds, seeds = seeds, adaptive = adaptive, trim = trim, allowParallel = allowParallel) }
參數(shù)詳解
trainControl | 所有參數(shù)詳解 |
---|---|
method | 重抽樣方法:Bootstrap(有放回隨機抽樣) 、Bootstrap632(有放回隨機抽樣擴展) 、LOOCV(留一交叉驗證) 、LGOCV(蒙特卡羅交叉驗證) 、cv(k折交叉驗證) 、repeatedcv(重復(fù)的k折交叉驗證) 、optimism_boot(Efron, B., & Tibshirani, R. J. (1994). “An introduction to the bootstrap”, pages 249-252. CRC press.) 、none(僅使用一個訓練集擬合模型) 、oob(袋外估計:隨機森林、多元自適應(yīng)回歸樣條、樹模型、靈活判別分析、條件樹) |
number | 控制K折交叉驗證的數(shù)目或者Bootstrap和LGOCV的抽樣迭代次數(shù) |
repeats | 控制重復(fù)交叉驗證的次數(shù) |
p | LGOCV:控制訓練比例 |
verboseIter | 輸出訓練日志的邏輯變量 |
returnData | 邏輯變量,把數(shù)據(jù)保存到trainingData 中(str(trainControl) 查看) |
search | search = grid(網(wǎng)格搜索) ,random(隨機搜索) |
returnResamp | 包含以下值的字符串:final、all、none ,設(shè)定有多少抽樣性能度量被保存。 |
classProbs | 是否計算類別概率 |
summaryFunction | 根據(jù)重抽樣計算模型性能的函數(shù) |
selectionFunction | 選擇最優(yōu)參數(shù)的函數(shù) |
index | 指定重抽樣樣本(使用相同的重抽樣樣本評估不同的算法、模型) |
allowParallel | 是否允許并行 |
示例
library(mlbench) #使用包中的數(shù)據(jù) Warning message: 程輯包‘mlbench'是用R版本4.1.3 來建造的 > data(Sonar) > str(Sonar[, 1:10]) 'data.frame': 208 obs. of 10 variables: $ V1 : num 0.02 0.0453 0.0262 0.01 0.0762 0.0286 0.0317 0.0519 0.0223 0.0164 ... $ V2 : num 0.0371 0.0523 0.0582 0.0171 0.0666 0.0453 0.0956 0.0548 0.0375 0.0173 ... $ V3 : num 0.0428 0.0843 0.1099 0.0623 0.0481 ... $ V4 : num 0.0207 0.0689 0.1083 0.0205 0.0394 ... $ V5 : num 0.0954 0.1183 0.0974 0.0205 0.059 ... $ V6 : num 0.0986 0.2583 0.228 0.0368 0.0649 ... $ V7 : num 0.154 0.216 0.243 0.11 0.121 ... $ V8 : num 0.16 0.348 0.377 0.128 0.247 ... $ V9 : num 0.3109 0.3337 0.5598 0.0598 0.3564 ... $ V10: num 0.211 0.287 0.619 0.126 0.446 ...
數(shù)據(jù)分割:
library(caret) set.seed(998) inTraining <- createDataPartition(Sonar$Class, p = .75, list = FALSE) training <- Sonar[ inTraining,] #訓練集 testing <- Sonar[-inTraining,] #測試集
模型擬合:
fitControl <- trainControl(## 10折交叉驗證 method = "repeatedcv", number = 10, ## 重復(fù)10次 repeats = 1) set.seed(825) gbmFit1 <- train(Class ~ ., data = training, method = "gbm", # 助推樹 trControl = fitControl, verbose = FALSE) gbmFit1 Stochastic Gradient Boosting 157 samples 60 predictor 2 classes: 'M', 'R' No pre-processing Resampling: Cross-Validated (10 fold, repeated 10 times) Summary of sample sizes: 141, 142, 141, 142, 141, 142, ... Resampling results across tuning parameters: interaction.depth n.trees Accuracy Kappa 1 50 0.7935784 0.5797839 1 100 0.8171078 0.6290208 1 150 0.8219608 0.6383173 2 50 0.8041912 0.6027771 2 100 0.8296176 0.6544713 2 150 0.8283627 0.6520181 3 50 0.8110343 0.6170317 3 100 0.8301275 0.6551379 3 150 0.8310343 0.6577252 Tuning parameter 'shrinkage' was held constant at a value of 0.1 Tuning parameter 'n.minobsinnode' was held constant at a value of 10 Accuracy was used to select the optimal model using the largest value. The final values used for the model were n.trees = 150, interaction.depth = 3, shrinkage = 0.1 and n.minobsinnode = 10.
到此這篇關(guān)于R語言caret包trainControl函數(shù)詳解的文章就介紹到這了,更多相關(guān)R語言caret包trainControl函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解R語言MCMC:Metropolis-Hastings采樣用于回歸的貝葉斯估計
這篇文章主要介紹了R語言MCMC:Metropolis-Hastings采樣用于回歸的貝葉斯估計,本文通過圖文實例相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03R的ggplot2畫圖,去除灰色陰影和網(wǎng)格的方式
這篇文章主要介紹了R的ggplot2畫圖,去除灰色陰影和網(wǎng)格的方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04R包制作后出現(xiàn)not available for錯誤問題解決解決
這篇文章主要為大家介紹了R包制作后出現(xiàn)not available for...錯誤的問題解決方式,有需要的朋友,可以借鑒參考下,希望能夠有所幫助2021-11-11R語言數(shù)據(jù)類型與相應(yīng)運算的實現(xiàn)
本文主要介紹了R語言數(shù)據(jù)類型與相應(yīng)運算的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03