Ladění hyperparametrů

Machine Learning with Tree-Based Models in R

Sandro Raabe

Data Scientist

Hyperparametry

  • Ovlivňují tvar a složitost stromů
  • Parametry modelu nastavené před trénováním, které řídí jeho složitost

Hyperparametry rozhodovacích stromů v parsnip:

  • min_n: minimální počet vzorků nutných k rozdělení uzlu
  • tree_depth: maximální povolená hloubka stromu
  • cost_complexity: penalizace za složitost stromu
Machine Learning with Tree-Based Models in R

Proč ladit?

Výchozí hodnoty nastavené v parsnip:

decision_tree(min_n = 20, tree_depth = 30, cost_complexity = 0.01)
  • V mnoha případech fungují dobře, ale nemusí být optimální pro všechny datové sady

 

Cílem ladění hyperparametrů je nalezení optimální sady jejich hodnot.

Machine Learning with Tree-Based Models in R

Ladění s tidymodels pomocí balíčku tune

prázdná ladicí mřížka

Machine Learning with Tree-Based Models in R

Ladění s tidymodels

fiktivní specifikace

Machine Learning with Tree-Based Models in R

Ladění s tidymodels

ladicí mřížka s jedním natrénovaným modelem pro každý bod

Machine Learning with Tree-Based Models in R

Ladění s tidymodels

je vybrán nejlepší bod mřížky

Machine Learning with Tree-Based Models in R

Krok 1: Zástupné symboly: tune()

spec_untuned <- decision_tree(

min_n = tune(), tree_depth = tune()
) %>% set_engine("rpart") %>% set_mode("classification")
Decision Tree Model Specification 
(classification)


Main Arguments: tree_depth = tune() min_n = tune()
  • tune() označuje parametry k ladění
  • Zbytek specifikace jako obvykle
Machine Learning with Tree-Based Models in R

Krok 2: Vytvoření ladicí mřížky: grid_regular()

tree_grid <- grid_regular(

parameters(spec_untuned),
levels = 3 )
# A tibble: 9 x 2
  min_n tree_depth
1     2          1
2    21          1
3    40          1
4     2          8
5    21          8
6    40          8
7     2         15
8    21         15
9    40         15
  • Pomocná funkce parameters()
  • levels: počet bodů mřížky pro každý hyperparametr
Machine Learning with Tree-Based Models in R

Krok 3: Ladění mřížky: tune_grid()

  • Vytvoří model pro každý bod mřížky
  • Vyhodnotí každý model mimo vzorek (CV)

 

Použití a argumenty:

  • Neladěná specifikace stromu
  • Vzorec modelu
  • Záhyby CV
  • Ladicí mřížka
  • Seznam metrik zabalených v metric_set()
tune_results <- tune_grid(

spec_untuned,
outcome ~ .,
resamples = my_folds,
grid = tree_grid,
metrics = metric_set(accuracy))
Machine Learning with Tree-Based Models in R

Vizualizace výsledků ladění

autoplot(tune_results)

výsledky ladění

Machine Learning with Tree-Based Models in R

Krok 4: Použití nejlepších parametrů: finalize_model()

# Select the best performing parameters
final_params <- select_best(tune_results)

final_params
# A tibble: 1 x 3
    min_n    tree_depth    .config
    <int>         <int>      <chr>
1      2              8     Model4
# Plug them into the specification
best_spec <- finalize_model(spec_untuned, 
                            final_params)

best_spec
Decision Tree Model Specification 
                 (classification)

Main Arguments:
  tree_depth = 8
  min_n = 2

Computational engine: rpart
Machine Learning with Tree-Based Models in R

Pojďme ladit!

Machine Learning with Tree-Based Models in R

Preparing Video For Download...