当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R tune extract-tune 提取调整对象的元素


这些函数从调谐对象中提取各种元素。如果它们尚不存在,则会抛出错误。

用法

# S3 method for last_fit
extract_workflow(x, ...)

# S3 method for tune_results
extract_workflow(x, ...)

# S3 method for tune_results
extract_spec_parsnip(x, ...)

# S3 method for tune_results
extract_recipe(x, ..., estimated = TRUE)

# S3 method for tune_results
extract_fit_parsnip(x, ...)

# S3 method for tune_results
extract_fit_engine(x, ...)

# S3 method for tune_results
extract_mold(x, ...)

# S3 method for tune_results
extract_preprocessor(x, ...)

参数

x

tune_results 对象。

...

目前未使用。

estimated

是否应返回原始(不适合)配方或适合配方的逻辑。

tune tune_results, x 中提取的值,如说明部分所述。

细节

这些函数取代 extract_model()

例子

library(recipes)
library(rsample)
library(parsnip)

set.seed(6735)
tr_te_split <- initial_split(mtcars)

spline_rec <- recipe(mpg ~ ., data = mtcars) %>%
  step_ns(disp)

lin_mod <- linear_reg() %>%
  set_engine("lm")

spline_res <- last_fit(lin_mod, spline_rec, split = tr_te_split)

extract_preprocessor(spline_res)
#> 
#> ── Recipe ────────────────────────────────────────────────────────────────
#> 
#> ── Inputs 
#> Number of variables by role
#> outcome:    1
#> predictor: 10
#> 
#> ── Operations 
#> • Natural splines on: disp

# The `spec` is the parsnip spec before it has been fit.
# The `fit` is the fitted parsnip model.
extract_spec_parsnip(spline_res)
#> Linear Regression Model Specification (regression)
#> 
#> Computational engine: lm 
#> 
extract_fit_parsnip(spline_res)
#> parsnip model object
#> 
#> 
#> Call:
#> stats::lm(formula = ..y ~ ., data = data)
#> 
#> Coefficients:
#> (Intercept)          cyl           hp         drat           wt  
#>   23.087028     0.326218     0.005969    -0.009576    -0.902839  
#>        qsec           vs           am         gear         carb  
#>    0.185826     1.492756     4.101555     0.174875    -1.278962  
#>   disp_ns_1    disp_ns_2  
#>  -15.149506    -4.905087  
#> 
extract_fit_engine(spline_res)
#> 
#> Call:
#> stats::lm(formula = ..y ~ ., data = data)
#> 
#> Coefficients:
#> (Intercept)          cyl           hp         drat           wt  
#>   23.087028     0.326218     0.005969    -0.009576    -0.902839  
#>        qsec           vs           am         gear         carb  
#>    0.185826     1.492756     4.101555     0.174875    -1.278962  
#>   disp_ns_1    disp_ns_2  
#>  -15.149506    -4.905087  
#> 

# The mold is returned from `hardhat::mold()`, and contains the
# predictors, outcomes, and information about the preprocessing
# for use on new data at `predict()` time.
extract_mold(spline_res)
#> $predictors
#> # A tibble: 24 × 11
#>      cyl    hp  drat    wt  qsec    vs    am  gear  carb disp_ns_1
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>     <dbl>
#>  1     8   205  2.93  5.25  18.0     0     0     3     4    0.341 
#>  2     4    95  3.92  3.15  22.9     1     0     4     2    0.260 
#>  3     6   175  3.62  2.77  15.5     0     1     5     6    0.274 
#>  4     8   245  3.73  3.84  15.4     0     0     3     4    0.554 
#>  5     4    52  4.93  1.62  18.5     1     1     4     2    0.0177
#>  6     8   180  3.07  3.78  18       0     0     3     3    0.571 
#>  7     8   215  3     5.42  17.8     0     0     3     4    0.366 
#>  8     8   175  3.15  3.44  17.0     0     0     3     2    0.542 
#>  9     8   180  3.07  4.07  17.4     0     0     3     3    0.571 
#> 10     6   110  3.9   2.88  17.0     0     1     4     4    0.324 
#> # ℹ 14 more rows
#> # ℹ 1 more variable: disp_ns_2 <dbl>
#> 
#> $outcomes
#> # A tibble: 24 × 1
#>      mpg
#>    <dbl>
#>  1  10.4
#>  2  22.8
#>  3  19.7
#>  4  13.3
#>  5  30.4
#>  6  15.2
#>  7  10.4
#>  8  18.7
#>  9  16.4
#> 10  21  
#> # ℹ 14 more rows
#> 
#> $blueprint
#> Recipe blueprint: 
#>  
#> # Predictors: 10 
#>   # Outcomes: 1 
#>    Intercept: FALSE 
#> Novel Levels: FALSE 
#>  Composition: tibble 
#> 
#> $extras
#> $extras$roles
#> NULL
#> 
#> 

# A useful shortcut is to extract the fitted recipe from the workflow
extract_recipe(spline_res)
#> 
#> ── Recipe ────────────────────────────────────────────────────────────────
#> 
#> ── Inputs 
#> Number of variables by role
#> outcome:    1
#> predictor: 10
#> 
#> ── Training information 
#> Training data contained 24 data points and no incomplete rows.
#> 
#> ── Operations 
#> • Natural splines on: disp | Trained

# That is identical to
identical(
  extract_mold(spline_res)$blueprint$recipe,
  extract_recipe(spline_res)
)
#> [1] TRUE
源代码:R/extract.R

相关用法


注:本文由纯净天空筛选整理自Max Kuhn等大神的英文原创作品 Extract elements of tune objects。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。