反转转换配方步骤标准化和记录的优雅方法?

问题描述

转换回由配方转换的 outcome(在本例中为 mpg)列的最优雅方法是什么? 解决方案可以是通用的(如果存在或仅适用于 lognormalize 步骤(代码如下)。

可能有用的链接
here 讨论了通用解决方案,但我认为它尚未实施。
提供了 R 函数 scale解决方here,但我不确定在这种情况下是否可以提供帮助。

library(recipes)

data <- tibble(mtcars) %>% 
    select(cyl,mpg)

rec <- recipe(mpg ~ .,data = data) %>%
    step_log(all_numeric()) %>%
    step_normalize(all_numeric()) %>%
    prep()

data_baked <- bake(rec,new_data = data)

# model fitting,predictions,etc...

# how to invert/transform back predictions (estimates) and true outcomes

解决方法

从配方转换 is to tidy() the recipe 中获取您需要的任何值的方法,然后使用 dplyr 动词获取您需要的值。

library(recipes)
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter,lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect,setdiff,setequal,union
#> 
#> Attaching package: 'recipes'
#> The following object is masked from 'package:stats':
#> 
#>     step

data <- tibble(mtcars) %>% 
  select(cyl,mpg)

rec <- recipe(mpg ~ .,data = data) %>%
  step_log(all_numeric()) %>%
  step_normalize(all_numeric(),id = "normalize_num") %>%
  prep()

两种方法可以导出配方步骤,然后您可以tidy()使用参数:

## notice that you can identify steps by `number` or `id`
tidy(rec)
#> # A tibble: 2 x 6
#>   number operation type      trained skip  id           
#>    <int> <chr>     <chr>     <lgl>   <lgl> <chr>        
#> 1      1 step      log       TRUE    FALSE log_LYuaY    
#> 2      2 step      normalize TRUE    FALSE normalize_num

## choose by number
tidy(rec,number = 1)
#> # A tibble: 2 x 3
#>   terms  base id       
#>   <chr> <dbl> <chr>    
#> 1 cyl    2.72 log_LYuaY
#> 2 mpg    2.72 log_LYuaY
## choose by id,which we set above (otherwise it has random id like log)
tidy(rec,id = "normalize_num")
#> # A tibble: 4 x 4
#>   terms statistic value id           
#>   <chr> <chr>     <dbl> <chr>        
#> 1 cyl   mean      1.78  normalize_num
#> 2 mpg   mean      2.96  normalize_num
#> 3 cyl   sd        0.309 normalize_num
#> 4 mpg   sd        0.298 normalize_num

一旦我们知道我们想要哪个步骤,我们就可以使用 dplyr 动词来准确地找出我们想要转换回来的值,比如 mpg 的平均值。

## extract out value
tidy(rec,id = "normalize_num") %>%
  filter(terms == "mpg",statistic == "mean") %>%
  pull(value)
#>      mpg 
#> 2.957514

reprex package (v0.3.0) 于 2021 年 1 月 25 日创建

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...