r - 似乎无法删除食谱中的变量

标签 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)

我是不是想错了?我也可以预先过滤,但我认为我的食谱应该包括类似的内容。

最佳答案

您应该在 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 创建于 2019-08-04 (v0.2.1)

关于r - 似乎无法删除食谱中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54489316/

相关文章:

r - tidymodels metric_set :Error: All inputs to `metric_set()` must be functions. 这些输入不是:(2)

r - 步骤将固定值转变为 tidymodels 中的变量列表

r - 使用交叉验证比较不同预测变量的线性回归模型的性能

r - R 函数 `poly` 到底有什么作用?

r - 使用 R 从列表列表中提取值

r - 一个 R ggplot2 图中的多个数据点

r - 使用 tidymodels 调整工作流集时如何正确设置参数网格?

r - 使用 roc_curve() 绘制 tidymodels 结果会收到数字与字符错误

r - 计算二维密度表面上点的概率