重要的是要意识到fit_resamples()
它的目的是衡量绩效。您训练的模型fit_resamples()
不会保留或以后使用。
假设您知道要用于 SVM 模型的参数。
library(tidymodels)
#> ── Attaching packages ─────────────────────────── tidymodels 0.1.1 ──
#> ✓ broom 0.7.0 ✓ recipes 0.1.13
#> ✓ dials 0.0.8 ✓ rsample 0.0.7
#> ✓ dplyr 1.0.0 ✓ tibble 3.0.3
#> ✓ ggplot2 3.3.2 ✓ tidyr 1.1.0
#> ✓ infer 0.5.3 ✓ tune 0.1.1
#> ✓ modeldata 0.0.2 ✓ workflows 0.1.2
#> ✓ parsnip 0.1.2 ✓ yardstick 0.0.7
#> ✓ purrr 0.3.4
#> ── Conflicts ────────────────────────────── tidymodels_conflicts() ──
#> x purrr::discard() masks scales::discard()
#> x dplyr::filter() masks stats::filter()
#> x dplyr::lag() masks stats::lag()
#> x recipes::step() masks stats::step()
## pretend this is your training data
data("hpc_data")
svm_spec <- svm_poly(degree = 1, cost = 1/4) %>%
set_engine("kernlab") %>%
set_mode("regression")
svm_wf <- workflow() %>%
add_model(svm_spec) %>%
add_formula(compounds ~ .)
hpc_folds <- vfold_cv(hpc_data)
svm_rs <- svm_wf %>%
fit_resamples(
resamples = hpc_folds
)
svm_rs
#> # Resampling results
#> # 10-fold cross-validation
#> # A tibble: 10 x 4
#> splits id .metrics .notes
#> <list> <chr> <list> <list>
#> 1 <split [3.9K/434]> Fold01 <tibble [2 × 3]> <tibble [0 × 1]>
#> 2 <split [3.9K/433]> Fold02 <tibble [2 × 3]> <tibble [0 × 1]>
#> 3 <split [3.9K/433]> Fold03 <tibble [2 × 3]> <tibble [0 × 1]>
#> 4 <split [3.9K/433]> Fold04 <tibble [2 × 3]> <tibble [0 × 1]>
#> 5 <split [3.9K/433]> Fold05 <tibble [2 × 3]> <tibble [0 × 1]>
#> 6 <split [3.9K/433]> Fold06 <tibble [2 × 3]> <tibble [0 × 1]>
#> 7 <split [3.9K/433]> Fold07 <tibble [2 × 3]> <tibble [0 × 1]>
#> 8 <split [3.9K/433]> Fold08 <tibble [2 × 3]> <tibble [0 × 1]>
#> 9 <split [3.9K/433]> Fold09 <tibble [2 × 3]> <tibble [0 × 1]>
#> 10 <split [3.9K/433]> Fold10 <tibble [2 × 3]> <tibble [0 × 1]>
此输出中没有拟合模型。模型适合每个重采样,但您不想将它们用于任何事情;它们被丢弃是因为它们的唯一目的是计算.metrics
估计性能。
如果您希望使用模型来预测新数据,则需要返回整个训练集并再次使用整个训练集拟合您的模型。
svm_fit <- svm_wf %>%
fit(hpc_data)
svm_fit
#> ══ Workflow [trained] ═══════════════════════════════════════════════
#> Preprocessor: Formula
#> Model: svm_poly()
#>
#> ── Preprocessor ─────────────────────────────────────────────────────
#> compounds ~ .
#>
#> ── Model ────────────────────────────────────────────────────────────
#> Support Vector Machine object of class "ksvm"
#>
#> SV type: eps-svr (regression)
#> parameter : epsilon = 0.1 cost C = 0.25
#>
#> Polynomial kernel function.
#> Hyperparameters : degree = 1 scale = 1 offset = 1
#>
#> Number of Support Vectors : 2827
#>
#> Objective Function Value : -284.7255
#> Training error : 0.835421
由reprex 包于 2020-07-17 创建(v0.3.0)
这个最终对象是您可以pull_workflow_fit()
用于可变重要性或类似的对象。