V-fold 交叉驗證(也稱為 k-fold 交叉驗證)將數據隨機分成大小大致相等的 V 組(稱為 "folds")。分析數據的重采樣由 V-1 個折疊組成,而評估集包含最終折疊。在基本的 V-fold 交叉驗證(即無重複)中,重新采樣的次數等於 V。
參數
- data
-
一個 DataFrame 。
- v
-
數據集的分區數。
- repeats
-
重複 V-fold 分區的次數。
- strata
-
data
中的變量(單個字符或名稱)用於進行分層抽樣。如果不是NULL
,則每次重新采樣都會在分層變量中創建。數字strata
被分為四分位數。 - breaks
-
給出對數值分層變量進行分層所需的箱數的單個數字。
- pool
-
用於確定特定組是否太小的數據比例,是否應合並到另一個組中。我們不建議將此參數降低到默認值 0.1 以下,因為分層組太小存在危險。
- ...
-
這些點用於將來的擴展,並且必須為空。
值
帶有類 vfold_cv
、 rset
、 tbl_df
、 tbl
和 data.frame
的 tibble。結果包括一列數據分割對象和一個或多個標識變量。對於單個重複,將有一個名為 id
的列,其中包含帶有折疊標識符的字符串。對於重複,id
是重複編號,還有一個名為 id2
的附加列
包含折疊信息(重複內)。
細節
如果重複多次,每次都會進行基本的V-fold交叉驗證。例如,如果 v = 10
使用 3 次重複,則總共有 30 個分割:分別生成 3 組,每組 10 個。
使用 strata
參數,在分層變量內進行隨機抽樣。這有助於確保重采樣與原始數據集具有相同的比例。對於分類變量,采樣是在每個類別內單獨進行的。對於數字分層變量,strata
被分為四分位數,然後用於分層。低於總數10%的地層合並在一起;有關更多詳細信息,請參閱make_strata()
。
例子
vfold_cv(mtcars, v = 10)
#> # 10-fold cross-validation
#> # A tibble: 10 × 2
#> splits id
#> <list> <chr>
#> 1 <split [28/4]> Fold01
#> 2 <split [28/4]> Fold02
#> 3 <split [29/3]> Fold03
#> 4 <split [29/3]> Fold04
#> 5 <split [29/3]> Fold05
#> 6 <split [29/3]> Fold06
#> 7 <split [29/3]> Fold07
#> 8 <split [29/3]> Fold08
#> 9 <split [29/3]> Fold09
#> 10 <split [29/3]> Fold10
vfold_cv(mtcars, v = 10, repeats = 2)
#> # 10-fold cross-validation repeated 2 times
#> # A tibble: 20 × 3
#> splits id id2
#> <list> <chr> <chr>
#> 1 <split [28/4]> Repeat1 Fold01
#> 2 <split [28/4]> Repeat1 Fold02
#> 3 <split [29/3]> Repeat1 Fold03
#> 4 <split [29/3]> Repeat1 Fold04
#> 5 <split [29/3]> Repeat1 Fold05
#> 6 <split [29/3]> Repeat1 Fold06
#> 7 <split [29/3]> Repeat1 Fold07
#> 8 <split [29/3]> Repeat1 Fold08
#> 9 <split [29/3]> Repeat1 Fold09
#> 10 <split [29/3]> Repeat1 Fold10
#> 11 <split [28/4]> Repeat2 Fold01
#> 12 <split [28/4]> Repeat2 Fold02
#> 13 <split [29/3]> Repeat2 Fold03
#> 14 <split [29/3]> Repeat2 Fold04
#> 15 <split [29/3]> Repeat2 Fold05
#> 16 <split [29/3]> Repeat2 Fold06
#> 17 <split [29/3]> Repeat2 Fold07
#> 18 <split [29/3]> Repeat2 Fold08
#> 19 <split [29/3]> Repeat2 Fold09
#> 20 <split [29/3]> Repeat2 Fold10
library(purrr)
data(wa_churn, package = "modeldata")
set.seed(13)
folds1 <- vfold_cv(wa_churn, v = 5)
map_dbl(
folds1$splits,
function(x) {
dat <- as.data.frame(x)$churn
mean(dat == "Yes")
}
)
#> [1] 0.2649982 0.2660632 0.2609159 0.2679681 0.2669033
set.seed(13)
folds2 <- vfold_cv(wa_churn, strata = churn, v = 5)
map_dbl(
folds2$splits,
function(x) {
dat <- as.data.frame(x)$churn
mean(dat == "Yes")
}
)
#> [1] 0.2653532 0.2653532 0.2653532 0.2653532 0.2654365
set.seed(13)
folds3 <- vfold_cv(wa_churn, strata = tenure, breaks = 6, v = 5)
map_dbl(
folds3$splits,
function(x) {
dat <- as.data.frame(x)$churn
mean(dat == "Yes")
}
)
#> [1] 0.2656250 0.2661104 0.2652228 0.2638396 0.2660518
相關用法
- R rsample validation_set 創建驗證拆分以進行調整
- R rsample validation_split 創建驗證集
- R rsample initial_split 簡單的訓練/測試集分割
- R rsample populate 添加評估指標
- R rsample int_pctl 自舉置信區間
- R rsample rset_reconstruct 使用新的 rset 子類擴展 rsample
- R rsample group_mc_cv 小組蒙特卡羅交叉驗證
- R rsample group_vfold_cv V 組交叉驗證
- R rsample rolling_origin 滾動原點預測重采樣
- R rsample reverse_splits 反轉分析和評估集
- R rsample group_bootstraps 團體自舉
- R rsample labels.rset 從 rset 對象中查找標簽
- R rsample get_fingerprint 獲取重采樣的標識符
- R rsample bootstraps 引導抽樣
- R rsample reg_intervals 具有線性參數模型的置信區間的便捷函數
- R rsample clustering_cv 集群交叉驗證
- R rsample initial_validation_split 創建初始訓練/驗證/測試拆分
- R rsample get_rsplit 從 rset 中檢索單個 rsplit 對象
- R rsample loo_cv 留一交叉驗證
- R rsample complement 確定評估樣本
- R rsample slide-resampling 基於時間的重采樣
- R rsample as.data.frame.rsplit 將 rsplit 對象轉換為 DataFrame
- R rsample labels.rsplit 從 rsplit 對象中查找標簽
- R rsample mc_cv 蒙特卡羅交叉驗證
- R rsample tidy.rsplit 整潔的重采樣對象
注:本文由純淨天空篩選整理自Hannah Frick等大神的英文原創作品 V-Fold Cross-Validation。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。