似乎无法删除配方中的变量

时间:2019-02-02 01:59:03

标签: r r-recipes tidymodels

我是recipes的新手,并且API出现了一些问题。删除某些我不感兴趣的功能后,为什么我不能bakejuice进行食谱操作?

set.seed(999)
train_test_split <- initial_split(mtcars)

mtcars_train <- training(train_test_split)
mtcars_test <- testing(train_test_split)

mtcars_train %>%
    recipe(mpg ~ cyl + disp + hp + gear) %>% 
    step_rm(qsec, vs, carb) %>% 
    step_center(all_numeric())  %>%
    step_scale(all_numeric()) %>%
    prep(training = mtcars_train)

导致:

Error in .f(.x[[i]], ...) : object 'qsec' not found

这很烦人,因为这意味着在应用步骤之后,我需要手动删除测试集和训练集上的行:

rec_scale <- mtcars %>%
    recipe(mpg ~ cyl + disp + hp + gear) %>% 
    step_center(all_numeric())  %>%
    step_scale(all_numeric()) %>%
    prep(training = mtcars_train)
train <- juice(rec_scale) %>%
  select(-qsec, -vs, -carb)
test <- bake(rec_scale, mtcars_test) %>%
  select(-qsec, -vs, -carb)

我在想这个错误吗?我可以选择预先过滤,但我认为我的食谱应包括类似内容。

1 个答案:

答案 0 :(得分:1)

您应在recipe()调用内包括配方步骤中使用的所有列。如果不在配方中,则无法将其删除。

library(tidymodels)
#> ── Attaching packages ────────────────────────────── tidymodels 0.0.2 ──
#> ✔ broom     0.5.2       ✔ purrr     0.3.2  
#> ✔ dials     0.0.2       ✔ recipes   0.1.6  
#> ✔ dplyr     0.8.3       ✔ rsample   0.0.5  
#> ✔ ggplot2   3.2.0       ✔ tibble    2.1.3  
#> ✔ infer     0.4.0.1     ✔ yardstick 0.0.3  
#> ✔ parsnip   0.0.3
#> ── Conflicts ───────────────────────────────── tidymodels_conflicts() ──
#> ✖ purrr::discard() masks scales::discard()
#> ✖ dplyr::filter()  masks stats::filter()
#> ✖ dplyr::lag()     masks stats::lag()
#> ✖ recipes::step()  masks stats::step()

set.seed(999)
train_test_split <- initial_split(mtcars)

mtcars_train <- training(train_test_split)
mtcars_test <- testing(train_test_split)

rec <- 
  mtcars_train %>%
  recipe(mpg ~ cyl + disp + hp + gear) %>% 
  step_center(all_numeric())  %>%
  step_scale(all_numeric()) %>%
  prep(training = mtcars_train)

summary(rec)
#> # A tibble: 5 x 4
#>   variable type    role      source  
#>   <chr>    <chr>   <chr>     <chr>   
#> 1 cyl      numeric predictor original
#> 2 disp     numeric predictor original
#> 3 hp       numeric predictor original
#> 4 gear     numeric predictor original
#> 5 mpg      numeric outcome   original

reprex package(v0.2.1)

创建于2019-08-04