부스팅 앙상블 최적화

R로 배우는 트리 기반 Machine Learning

Sandro Raabe

Data Scientist

출발점: 미튜닝 성능

collect_metrics(cv_results)
# A tibble: 1 x 3
  .metric   .mean       n   
  <chr>     <dbl>   <int>             
1 roc_auc   0.951       5  
  • 95% - 튜닝 전 모델치고 나쁘지 않습니다!
R로 배우는 트리 기반 Machine Learning

튜닝 워크플로우

tune 함수

R로 배우는 트리 기반 Machine Learning

튜닝 워크플로우

그리드 생성

R로 배우는 트리 기반 Machine Learning

튜닝 워크플로우

교차 검증

R로 배우는 트리 기반 Machine Learning

튜닝 워크플로우

tune_grid 함수

R로 배우는 트리 기반 Machine Learning

튜닝 워크플로우

select_best 함수

R로 배우는 트리 기반 Machine Learning

튜닝 워크플로우

튜닝 워크플로우

R로 배우는 트리 기반 Machine Learning

튜닝 워크플로우

final fit 함수

R로 배우는 트리 기반 Machine Learning

1단계: 튜닝 스펙 생성

# Create the specification with placeholders
boost_spec <- boost_tree(

trees = 500, learn_rate = tune(), tree_depth = tune(), sample_size = tune()) %>%
set_mode("classification") %>% set_engine("xgboost")
Boosted Tree Model Specification (classification)

Main Arguments:
  trees = 500
  tree_depth = tune()
  learn_rate = tune()
  sample_size = tune()

Computational engine: xgboost
  • 일반적인 스펙
  • 차이점: tune()으로 튜닝할 값의 플레이스홀더 지정
R로 배우는 트리 기반 Machine Learning

2단계: 튜닝 그리드 생성

# Create a regular grid
tunegrid_boost <- grid_regular(parameters(boost_spec), 

levels = 2)
# A tibble: 8 x 3
  tree_depth     learn_rate sample_size
       <int>          <dbl>       <dbl>
1          1   0.0000000001         0.1
2         15   0.0000000001         0.1
3          1   0.1                  0.1
4         15   0.1                  0.1
5          1   0.0000000001         1  
6         15   0.0000000001         1  
7          1   0.1                  1  
8         15   0.1                  1
# Create a random grid
grid_random(parameters(boost_spec),

size = 8)
# A tibble: 8 x 3
  tree_depth    learn_rate   sample_size
       <int>         <dbl>         <dbl>
1         11   0.0000000249        0.858
2         12   0.00000000392       0.856
3         15   0.0000000131        0.220
4         15   0.0000216           0.125
5         10   0.00000000537       0.759
6         14   0.0395              0.270
7          2   0.000000828         0.904
8          9   0.0000254           0.473
R로 배우는 트리 기반 Machine Learning

3단계: 튜닝 실행

tune_grid()의 인자:

  • 더미 스펙
  • 모델 수식
  • 리샘플/폴드
  • 파라미터 그리드
  • metric_set()의 메트릭 목록

함수 호출:

# Tune along the grid
tune_results <- tune_grid(

boost_spec,
still_customer ~ .,
resamples = vfold_cv(customers_train, v = 6),
grid = tunegrid_boost,
metrics = metric_set(roc_auc))
R로 배우는 트리 기반 Machine Learning

결과 시각화

# Plot the results
autoplot(tune_results)

튜닝 결과

R로 배우는 트리 기반 Machine Learning

4단계: 모델 확정

# Select the final hyperparameters
best_params <- select_best(tune_results)

best_params
# A tibble: 1 x 4
  tree_depth learn_rate sample_size .config
       <int>      <dbl>       <dbl> <chr> 
1          8        0.1        0.55 Model17
# Finalize the specification
final_spec <- finalize_model(boost_spec, 
                             best_params)
final_spec
Boosted Tree Model Specification

Main Arguments:
  trees = 500
  tree_depth = 8
  learn_rate = 0.1
  sample_size = 0.55

Computational engine: xgboost
R로 배우는 트리 기반 Machine Learning

마지막 단계: 최종 모델 학습

final_model <- final_spec %>% fit(formula = still_customer ~ .,
                                  data = customers_train)

final_model
Fit time:  2.3s 
##### xgb.Booster
raw: 343.8 Kb 
nfeatures : 37 
evaluation_log:
    iter training_error
       1       0.046403                  
     100       0.002592
R로 배우는 트리 기반 Machine Learning

이제 직접 해보세요!

R로 배우는 트리 기반 Machine Learning

Preparing Video For Download...