single
Hyperparameter Tuning with Tune
Sveip for å vise menyen
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.
123456789101112131415161718192021222324252627282930313233343536options(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))
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.
Sveip for å begynne å kode
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 specifymetrics = metric_set(accuracy). - Select the best hyperparameter combination based on accuracy utilizing the
select_best()function. - Assign the best result to
best_resultand print it.
Løsning
Takk for tilbakemeldingene dine!
single
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår