Skip to content

Instantly share code, notes, and snippets.

@juliasilge
Created August 17, 2020 19:32
Show Gist options
  • Save juliasilge/72142c77feaf9e415d08ec197817993d to your computer and use it in GitHub Desktop.
Save juliasilge/72142c77feaf9e415d08ec197817993d to your computer and use it in GitHub Desktop.
Apply recipe to resamples
library(tidymodels)

car_rec <- recipe(~ ., data = mtcars) %>%
  step_normalize(disp, qsec)

car_prep <- prep(car_rec)

juice(car_prep)
#> # A tibble: 32 x 11
#>      mpg   cyl    disp    hp  drat    wt   qsec    vs    am  gear  carb
#>    <dbl> <dbl>   <dbl> <dbl> <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6 -0.571    110  3.9   2.62 -0.777     0     1     4     4
#>  2  21       6 -0.571    110  3.9   2.88 -0.464     0     1     4     4
#>  3  22.8     4 -0.990     93  3.85  2.32  0.426     1     1     4     1
#>  4  21.4     6  0.220    110  3.08  3.22  0.890     1     0     3     1
#>  5  18.7     8  1.04     175  3.15  3.44 -0.464     0     0     3     2
#>  6  18.1     6 -0.0462   105  2.76  3.46  1.33      1     0     3     1
#>  7  14.3     8  1.04     245  3.21  3.57 -1.12      0     0     3     4
#>  8  24.4     4 -0.678     62  3.69  3.19  1.20      1     0     4     2
#>  9  22.8     4 -0.726     95  3.92  3.15  2.83      1     0     4     2
#> 10  19.2     6 -0.509    123  3.92  3.44  0.253     1     0     4     4
#> # … with 22 more rows

car_folds <- vfold_cv(mtcars)

car_folds %>%
  mutate(juiced = map(splits, ~ bake(car_prep, new_data = analysis(.))))
#> #  10-fold cross-validation 
#> # A tibble: 10 x 3
#>    splits         id     juiced            
#>    <list>         <chr>  <list>            
#>  1 <split [28/4]> Fold01 <tibble [28 × 11]>
#>  2 <split [28/4]> Fold02 <tibble [28 × 11]>
#>  3 <split [29/3]> Fold03 <tibble [29 × 11]>
#>  4 <split [29/3]> Fold04 <tibble [29 × 11]>
#>  5 <split [29/3]> Fold05 <tibble [29 × 11]>
#>  6 <split [29/3]> Fold06 <tibble [29 × 11]>
#>  7 <split [29/3]> Fold07 <tibble [29 × 11]>
#>  8 <split [29/3]> Fold08 <tibble [29 × 11]>
#>  9 <split [29/3]> Fold09 <tibble [29 × 11]>
#> 10 <split [29/3]> Fold10 <tibble [29 × 11]>

Created on 2020-08-17 by the reprex package (v0.3.0.9001)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment