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

single

bookHyperparameter Tuning with Tune

Swipe um das Menü anzuzeigen

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.

Aufgabe

Wischen, um mit dem Codieren zu beginnen

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.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 8
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt