Náhodný les

Machine Learning with Tree-Based Models in R

Sandro Raabe

Data Scientist

Náhodný les

  • Vhodný pro vícerozměrná data
  • Snadné použití
  • Dobrý výkon bez ladění
  • Implementován v různých balíčcích: ranger, randomForest
  • Rozhraní tidymodels: rand_forest() (balíček parsnip)
Machine Learning with Tree-Based Models in R

Myšlenka

  • Základní myšlenka (stejná jako bagging): trénování stromů na bootstrapových vzorcích
  • Klíčový rozdíl: náhodné prediktory napříč stromy $\rightarrow$ náhodný les
Machine Learning with Tree-Based Models in R

Intuice

schéma náhodného lesa

Machine Learning with Tree-Based Models in R

Kód: Specifikace modelu náhodného lesa

  • Název funkce: rand_forest()

Hyperparametry:

  • mtry: prediktory viděné v každém uzlu, výchozí hodnota:
    $$\left\lfloor\sqrt\text{num predictors}\right\rfloor$$
  • trees: počet stromů v lese
  • min_n: nejmenší povolená velikost uzlu
rand_forest(

mtry = 4,
trees = 500,
min_n = 10) %>%
# Set the mode set_mode("classification") %>%
# Use engine ranger or randomForest set_engine("ranger")
Machine Learning with Tree-Based Models in R

Kód: Specifikace modelu náhodného lesa

spec <- rand_forest(trees = 100) %>%

set_mode("classification") %>%
set_engine("ranger")
Random Forest Model Specification

(classification)
Main Arguments: trees = 100
Computational engine: ranger
Machine Learning with Tree-Based Models in R

Trénování lesa

spec %>% fit(still_customer ~ ., data = customers_train)
parsnip model object

Fit time:  631ms 
Ranger result

Number of trees:                  100 
Sample size:                      9116 
Number of independent variables:  19 
Mtry:                             4 
Target node size:                 10
Machine Learning with Tree-Based Models in R

Důležitost proměnných

rand_forest(mode = "classification") %>%
    set_engine("ranger", importance = "impurity") %>%

fit(still_customer ~ ., data = customers_train) %>%
vip::vip()

graf důležitosti proměnných

Machine Learning with Tree-Based Models in R

Pojďme zasadit náhodný les!

Machine Learning with Tree-Based Models in R

Preparing Video For Download...