使用嵌入在 tidymodels 框架中进行目标编码
target encoding in the tidymodels framework using embedd
我想对级别过多的分类变量进行目标编码。
我看过这个 vignette ,它提出了以下目标编码变量的方法:
step_lencode_glm()
step_lencode_bayes()
step_lencode_mixed()
这三种方法使用所有记录来创建估计值,tends to overfit to that column。
使用 tidymodels,有没有一种简单的方法可以将我的训练集分成 5 折并从其他 4 折中获取目标编码?
谢谢
如果您使用像 fit_resamples()
这样的函数,就会发生这种情况;您将获得从拟合到 n - 1
折叠和最后一次评估的性能估计。
如果您想更详细地探讨这一点,可以follow along with this vignette。
library(tidymodels)
library(embed)
data(grants, package = "modeldata")
set.seed(1)
folds <- vfold_cv(grants_other, v = 3)
folds
#> # 3-fold cross-validation
#> # A tibble: 3 × 2
#> splits id
#> <list> <chr>
#> 1 <split [5460/2730]> Fold1
#> 2 <split [5460/2730]> Fold2
#> 3 <split [5460/2730]> Fold3
rec <-
recipe(class ~ sponsor_code, data = grants_other) %>%
step_lencode_glm(sponsor_code, outcome = vars(class))
res <-
folds %>%
mutate(recipe = map(splits, prepper, recipe = rec),
processed = map(recipe, tidy, number = 1))
res %>%
select(fold_id = id, processed) %>%
unnest(processed)
#> # A tibble: 757 × 5
#> fold_id level value terms id
#> <chr> <chr> <dbl> <chr> <chr>
#> 1 Fold1 100D 0.288 sponsor_code lencode_glm_gfHLA
#> 2 Fold1 101A -1.50 sponsor_code lencode_glm_gfHLA
#> 3 Fold1 103C -1.95 sponsor_code lencode_glm_gfHLA
#> 4 Fold1 105A -1.39 sponsor_code lencode_glm_gfHLA
#> 5 Fold1 107C 16.6 sponsor_code lencode_glm_gfHLA
#> 6 Fold1 10B 16.6 sponsor_code lencode_glm_gfHLA
#> 7 Fold1 111C -16.6 sponsor_code lencode_glm_gfHLA
#> 8 Fold1 112D 0.560 sponsor_code lencode_glm_gfHLA
#> 9 Fold1 113A 0.223 sponsor_code lencode_glm_gfHLA
#> 10 Fold1 118B 0 sponsor_code lencode_glm_gfHLA
#> # … with 747 more rows
由 reprex package (v2.0.1)
创建于 2022-02-22
我们建议像这样重采样以估计嵌入策略的性能,然后整个训练集适合最终嵌入。
我想对级别过多的分类变量进行目标编码。
我看过这个 vignette ,它提出了以下目标编码变量的方法:
step_lencode_glm()
step_lencode_bayes()
step_lencode_mixed()
这三种方法使用所有记录来创建估计值,tends to overfit to that column。
使用 tidymodels,有没有一种简单的方法可以将我的训练集分成 5 折并从其他 4 折中获取目标编码?
谢谢
如果您使用像 fit_resamples()
这样的函数,就会发生这种情况;您将获得从拟合到 n - 1
折叠和最后一次评估的性能估计。
如果您想更详细地探讨这一点,可以follow along with this vignette。
library(tidymodels)
library(embed)
data(grants, package = "modeldata")
set.seed(1)
folds <- vfold_cv(grants_other, v = 3)
folds
#> # 3-fold cross-validation
#> # A tibble: 3 × 2
#> splits id
#> <list> <chr>
#> 1 <split [5460/2730]> Fold1
#> 2 <split [5460/2730]> Fold2
#> 3 <split [5460/2730]> Fold3
rec <-
recipe(class ~ sponsor_code, data = grants_other) %>%
step_lencode_glm(sponsor_code, outcome = vars(class))
res <-
folds %>%
mutate(recipe = map(splits, prepper, recipe = rec),
processed = map(recipe, tidy, number = 1))
res %>%
select(fold_id = id, processed) %>%
unnest(processed)
#> # A tibble: 757 × 5
#> fold_id level value terms id
#> <chr> <chr> <dbl> <chr> <chr>
#> 1 Fold1 100D 0.288 sponsor_code lencode_glm_gfHLA
#> 2 Fold1 101A -1.50 sponsor_code lencode_glm_gfHLA
#> 3 Fold1 103C -1.95 sponsor_code lencode_glm_gfHLA
#> 4 Fold1 105A -1.39 sponsor_code lencode_glm_gfHLA
#> 5 Fold1 107C 16.6 sponsor_code lencode_glm_gfHLA
#> 6 Fold1 10B 16.6 sponsor_code lencode_glm_gfHLA
#> 7 Fold1 111C -16.6 sponsor_code lencode_glm_gfHLA
#> 8 Fold1 112D 0.560 sponsor_code lencode_glm_gfHLA
#> 9 Fold1 113A 0.223 sponsor_code lencode_glm_gfHLA
#> 10 Fold1 118B 0 sponsor_code lencode_glm_gfHLA
#> # … with 747 more rows
由 reprex package (v2.0.1)
创建于 2022-02-22我们建议像这样重采样以估计嵌入策略的性能,然后整个训练集适合最终嵌入。