Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Hyperparameter Tuning with Tune | Section
Predictive Modeling with Tidymodels in R
Sectie 1. Hoofdstuk 8
single

single

bookHyperparameter Tuning with Tune

Veeg om het menu te tonen

Hyperparameters are adjustable parameters that govern the training process of a model but are not learned from the data itself. These include settings like the number of trees in a random forest, the maximum depth of a decision tree, or the penalty term in regularized regression. Selecting appropriate hyperparameter values is crucial because they can significantly influence model performance. If set too high or too low, hyperparameters may cause overfitting or underfitting, leading to poor generalization on new data.

123456789101112131415161718192021222324252627282930313233343536
options(crayon.enabled = FALSE) library(tidymodels) tree_spec <- decision_tree( tree_depth = tune(), min_n = tune() ) %>% set_engine("rpart") %>% set_mode("classification") # Create a workflow tree_workflow <- workflow() %>% add_model(tree_spec) %>% add_formula(Species ~ .) # Define a grid of hyperparameters to search over tree_grid <- grid_regular( tree_depth(range = c(1, 5)), min_n(range = c(2, 10)), levels = 3 ) # Set up cross-validation folds set.seed(123) cv_folds <- vfold_cv(iris, v = 5) # Perform grid search tuning tune_results <- tune_grid( tree_workflow, resamples = cv_folds, grid = tree_grid, metrics = metric_set(accuracy) ) # Review the tuning results print(collect_metrics(tune_results))
copy

During hyperparameter tuning, it is important to use cross-validation to estimate model performance for each hyperparameter combination. This helps avoid overfitting to a single train-test split and provides a more robust assessment of how the model will perform on unseen data. Once tuning is complete, you can select the best model based on the performance metric most relevant to your problem, such as accuracy for classification or RMSE for regression. The selected model can then be finalized and retrained on the entire training set before being evaluated on the test set.

Taak

Veeg om te beginnen met coderen

Perform hyperparameter tuning for a decision tree classification model using a provided grid.

  • The decision tree specification, workflow, hyperparameter grid, and cross-validation folds are already set up for you.
  • Utilize the tune_grid() function to search over the hyperparameter grid. Pass the workflow, cross-validation folds (resamples), the grid, and specify metrics = metric_set(accuracy).
  • Select the best hyperparameter combination based on accuracy utilizing the select_best() function.
  • Assign the best result to best_result and print it.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 8
single

single

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

some-alt