我可以使用
retain=TRUE
函数的 prep()
参数将预处理后的训练数据存储到配方中。 prep()
的帮助页面强烈建议使用工作流程。但是在这种情况下我该如何传递 retain=TRUE
参数呢?
这是一个玩具示例。
library(tidymodels)
mod <- linear_reg()
rec <- recipe(displ ~ cyl + drv, data=mpg) %>%
step_filter(drv != "r")
wf <- workflow() %>% add_model(mod) %>% add_recipe(rec)
fitted <- wf %>% fit(data=mpg)
fitted %>% extract_recipe() %>% bake(new_data = NULL)
我希望这会返回预处理的数据,但我收到以下错误消息:
中的错误: 使用juice()
中的retain = TRUE
能够提取训练集。prep()
目前,您无法通过工作流程传递
prep
。
但是你可以使用
library(tidymodels)
mod <- linear_reg()
rec <-
recipe(disp ~ cyl + carb, data = mtcars) %>%
# add this show that the recipe was processed:
step_mutate(half_cyl = cyl / 2) %>%
step_filter(carb != 4)
wf <- workflow(rec, mod)
fitted <- wf %>% fit(data = mtcars)
fitted %>%
extract_recipe() %>%
bake(new_data = mtcars)
#> # A tibble: 32 × 4
#> cyl carb disp half_cyl
#> <dbl> <dbl> <dbl> <dbl>
#> 1 6 4 160 3
#> 2 6 4 160 3
#> 3 4 1 108 2
#> 4 6 1 258 3
#> 5 8 2 360 4
#> 6 6 1 225 3
#> 7 8 4 360 4
#> 8 4 2 147. 2
#> 9 4 2 141. 2
#> 10 6 4 168. 3
#> # ℹ 22 more rows
创建于 2024 年 10 月 22 日,使用 reprex v2.1.0