랜덤 포레스트 모델 구축 및 튜닝

Tidyverse로 배우는 Machine Learning

Dmitriy (Dima) Gorenshteyn

Lead Data Scientist, Memorial Sloan Kettering Cancer Center

교차 검증 성능

Tidyverse로 배우는 Machine Learning

교차 검증 성능

Tidyverse로 배우는 Machine Learning

교차 검증 성능

Tidyverse로 배우는 Machine Learning

교차 검증 성능

Tidyverse로 배우는 Machine Learning

선형 회귀 모델

 

검증 평균 절대 오차:

1.5년

Tidyverse로 배우는 Machine Learning

다른 모델

Tidyverse로 배우는 Machine Learning

랜덤 포레스트의 장점

  • 비선형 관계 처리 가능
  • 상호작용 처리 가능
Tidyverse로 배우는 Machine Learning

기본 랜덤 포레스트 도구

모델
rf_model <- ranger(formula = ___, data = ___, seed = ___)

 

예측
prediction <- predict(rf_model, new_data)$predictions
Tidyverse로 배우는 Machine Learning

기본 랜덤 포레스트 모델 구축

library(ranger)
cv_models_rf <- cv_data %>% 
 mutate(model = map(train, ~ranger(formula = life_expectancy~., 
                                    data = .x, seed = 42)))
cv_prep_rf <- cv_models_rf %>% 
 mutate(validate_predicted = map2(model, validate, 
                                  ~predict(.x, .y)$predictions))
Tidyverse로 배우는 Machine Learning

ranger 하이퍼파라미터

모델
rf_model <- ranger(formula, data, seed, mtry, num.trees)
하이퍼파라미터
이름 범위 기본값
mtry $1:number\ of\ features$ $\sqrt{number\ of\ feat}$
num.trees $1:\infty$ $500$
Tidyverse로 배우는 Machine Learning

하이퍼파라미터 튜닝

cv_tune <- cv_data %>% 
  crossing(mtry = 1:5)

cv_tune
# A tibble: 25 x 5
   splits       id    train                validate            mtry
   <list>       <chr> <list>               <list>             <int>
 1 <S3: rsplit> Fold1 <tibble [2,402 × 7]> <tibble [601 × 7]>     1
 2 <S3: rsplit> Fold1 <tibble [2,402 × 7]> <tibble [601 × 7]>     2
 3 <S3: rsplit> Fold1 <tibble [2,402 × 7]> <tibble [601 × 7]>     3
 4 <S3: rsplit> Fold1 <tibble [2,402 × 7]> <tibble [601 × 7]>     4
 5 <S3: rsplit> Fold1 <tibble [2,402 × 7]> <tibble [601 × 7]>     5
 6 <S3: rsplit> Fold2 <tibble [2,402 × 7]> <tibble [601 × 7]>     1
 7 <S3: rsplit> Fold2 <tibble [2,402 × 7]> <tibble [601 × 7]>     2
Tidyverse로 배우는 Machine Learning

하이퍼파라미터 튜닝

cv_model_tunerf <- cv_tune %>% 
  mutate(model = map2(train, mtry, ~ranger(formula = life_expectancy~., 
                                           data = .x, mtry = .y)))

cv_model_tunerf
# A tibble: 25 x 6
   splits       id    train                validate      mtry  model       
 * <list>       <chr> <list>               <list>        <int> <list>      
 1 <S3: rsplit> Fold1 <tibble [2,402 × 7]> <tibble [60…   1    <S3: ranger>
 2 <S3: rsplit> Fold1 <tibble [2,402 × 7]> <tibble [60…   2    <S3: ranger>
 3 <S3: rsplit> Fold1 <tibble [2,402 × 7]> <tibble [60…   3    <S3: ranger>
 4 <S3: rsplit> Fold1 <tibble [2,402 × 7]> <tibble [60…   4    <S3: ranger>
 5 <S3: rsplit> Fold1 <tibble [2,402 × 7]> <tibble [60…   5    <S3: ranger>
 6 <S3: rsplit> Fold2 <tibble [2,402 × 7]> <tibble [60…   1    <S3: ranger>
 7 <S3: rsplit> Fold2 <tibble [2,402 × 7]> <tibble [60…   2    <S3: ranger>
Tidyverse로 배우는 Machine Learning

연습해 봅시다!

Tidyverse로 배우는 Machine Learning

Preparing Video For Download...