fit_best | R Documentation |
fit_best()
takes the results from model tuning and fits it to the training
set using tuning parameters associated with the best performance.
fit_best(x, ...)
## Default S3 method:
fit_best(x, ...)
## S3 method for class 'tune_results'
fit_best(
x,
metric = NULL,
parameters = NULL,
verbose = FALSE,
add_validation_set = NULL,
...
)
x |
The results of class |
... |
Not currently used. |
metric |
A character string (or |
parameters |
An optional 1-row tibble of tuning parameter settings, with
a column for each tuning parameter. This tibble should have columns for each
tuning parameter identifier (e.g. |
verbose |
A logical for printing logging. |
add_validation_set |
When the resamples embedded in |
This function is a shortcut for the manual steps of:
best_param <- select_best(tune_results, metric) # or other `select_*()` wflow <- finalize_workflow(wflow, best_param) # or just `finalize_model()` wflow_fit <- fit(wflow, data_set)
In comparison to last_fit()
, that function requires a finalized model, fits
the model on the training set defined by rsample::initial_split()
, and
computes metrics from the test set.
A fitted workflow.
library(recipes)
library(rsample)
library(parsnip)
library(dplyr)
data(meats, package = "modeldata")
meats <- meats %>% select(-water, -fat)
set.seed(1)
meat_split <- initial_split(meats)
meat_train <- training(meat_split)
meat_test <- testing(meat_split)
set.seed(2)
meat_rs <- vfold_cv(meat_train, v = 10)
pca_rec <-
recipe(protein ~ ., data = meat_train) %>%
step_normalize(all_numeric_predictors()) %>%
step_pca(all_numeric_predictors(), num_comp = tune())
knn_mod <- nearest_neighbor(neighbors = tune()) %>% set_mode("regression")
ctrl <- control_grid(save_workflow = TRUE)
set.seed(128)
knn_pca_res <-
tune_grid(knn_mod, pca_rec, resamples = meat_rs, grid = 10, control = ctrl)
knn_fit <- fit_best(knn_pca_res, verbose = TRUE)
predict(knn_fit, meat_test)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.