Home > Software design >  Normalizing outputs from recipes in result in no changes to numeric predictor data in R
Normalizing outputs from recipes in result in no changes to numeric predictor data in R

Time:11-13

When I try to use any of the 'step' functions in a recipe in R and check the results) (using head(df), none of the numbers are shown to have changed.

Using iris as an example, I tried to normalize the numeric predictors from 0-1. When I call the output under head(df) nothing changes in the results(no errors are noted), example below:

Original attempt:

iris_recipe<-iris %>% 
     recipe(Species ~ .) %>% 
     step_range(recipe, all_numeric_predictors(), min = 0, max=1)

head(iris_recipe)
$var_info
# A tibble: 5 × 4
  variable     type    role      source  
  <chr>        <chr>   <chr>     <chr>   
1 Sepal.Length numeric predictor original
2 Sepal.Width  numeric predictor original
3 Petal.Length numeric predictor original
4 Petal.Width  numeric predictor original
5 Species      nominal outcome   original

$term_info
# A tibble: 5 × 4
  variable     type    role      source  
  <chr>        <chr>   <chr>     <chr>   
1 Sepal.Length numeric predictor original
2 Sepal.Width  numeric predictor original
3 Petal.Length numeric predictor original
4 Petal.Width  numeric predictor original
5 Species      nominal outcome   original

$steps
$steps[[1]]
$terms
<list_of<quosure>>

[[1]]
<quosure>
expr: ^recipe
env:  0x000002b201dc23e0

[[2]]
<quosure>
expr: ^all_numeric_predictors()
env:  0x000002b201dc23e0


$role
[1] NA

$trained
[1] FALSE

$min
[1] 0

$max
[1] 1

$ranges
NULL

$skip
[1] FALSE

$id
[1] "range_5JGit"

attr(,"class")
[1] "step_range" "step"      


$template
# A tibble: 150 × 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1          5.1         3.5          1.4         0.2 setosa 
 2          4.9         3            1.4         0.2 setosa 
 3          4.7         3.2          1.3         0.2 setosa 
 4          4.6         3.1          1.5         0.2 setosa 
 5          5           3.6          1.4         0.2 setosa 
 6          5.4         3.9          1.7         0.4 setosa 
 7          4.6         3.4          1.4         0.3 setosa 
 8          5           3.4          1.5         0.2 setosa 
 9          4.4         2.9          1.4         0.2 setosa 
10          4.9         3.1          1.5         0.1 setosa 
# … with 140 more rows
# ℹ Use `print(n = ...)` to see more rows

$levels
NULL

$retained
[1] NA

Specific Attempt:

iris_recipe2<-iris %>% 
     recipe(Species ~ .) %>% 
     step_range(recipe, Sepal.Length, min=0, max=1)

head(iris_recipe2)
$var_info
# A tibble: 5 × 4
  variable     type    role      source  
  <chr>        <chr>   <chr>     <chr>   
1 Sepal.Length numeric predictor original
2 Sepal.Width  numeric predictor original
3 Petal.Length numeric predictor original
4 Petal.Width  numeric predictor original
5 Species      nominal outcome   original

$term_info
# A tibble: 5 × 4
  variable     type    role      source  
  <chr>        <chr>   <chr>     <chr>   
1 Sepal.Length numeric predictor original
2 Sepal.Width  numeric predictor original
3 Petal.Length numeric predictor original
4 Petal.Width  numeric predictor original
5 Species      nominal outcome   original

$steps
$steps[[1]]
$terms
<list_of<quosure>>

[[1]]
<quosure>
expr: ^recipe
env:  0x000002b2073fe328

[[2]]
<quosure>
expr: ^Sepal.Length
env:  0x000002b2073fe328


$role
[1] NA

$trained
[1] FALSE

$min
[1] 0

$max
[1] 1

$ranges
NULL

$skip
[1] FALSE

$id
[1] "range_p7JFo"

attr(,"class")
[1] "step_range" "step"      


$template
# A tibble: 150 × 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1          5.1         3.5          1.4         0.2 setosa 
 2          4.9         3            1.4         0.2 setosa 
 3          4.7         3.2          1.3         0.2 setosa 
 4          4.6         3.1          1.5         0.2 setosa 
 5          5           3.6          1.4         0.2 setosa 
 6          5.4         3.9          1.7         0.4 setosa 
 7          4.6         3.4          1.4         0.3 setosa 
 8          5           3.4          1.5         0.2 setosa 
 9          4.4         2.9          1.4         0.2 setosa 
10          4.9         3.1          1.5         0.1 setosa 
# … with 140 more rows
# ℹ Use `print(n = ...)` to see more rows

$levels
NULL

$retained
[1] NA

CodePudding user response:

After creating a recipe specification we can prep the recipe and bake our data to apply the recipe.

library(tidyverse)
library(tidymodels)

iris_processed <- iris %>% 
  recipe(Species ~ .) %>% 
  step_range(all_numeric_predictors(), min = 0, max=1) %>%
  prep() %>%
  bake(iris)

head(iris)
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa
#> 5          5.0         3.6          1.4         0.2  setosa
#> 6          5.4         3.9          1.7         0.4  setosa
head(iris_processed)
#> # A tibble: 6 × 5
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#> 1       0.222        0.625       0.0678      0.0417 setosa 
#> 2       0.167        0.417       0.0678      0.0417 setosa 
#> 3       0.111        0.5         0.0508      0.0417 setosa 
#> 4       0.0833       0.458       0.0847      0.0417 setosa 
#> 5       0.194        0.667       0.0678      0.0417 setosa 
#> 6       0.306        0.792       0.119       0.125  setosa

Created on 2022-11-12 with reprex v2.0.2

  • Related