R에서 tidymodels로 모델링하기
David Svancer
Data Scientist
모델 학습 전에 설정되며 모델 복잡도를 제어하는 파라미터
parsnip 결정 트리
cost_complexitytree_depthmin_n
decision_tree() 함수는 기본 하이퍼파라미터 값 설정
cost_complexity는 0.01로 설정tree_depth는 30으로 설정min_n은 20으로 설정이 값이 모든 데이터셋에 최적이지 않을 수 있음
dt_model <- decision_tree() %>%
set_engine('rpart') %>%
set_mode('classification')
tune 패키지의 tune() 함수
parsnip 모델 명세에서 tune()으로 설정dt_tune_model <- decision_tree(cost_complexity = tune(), tree_depth = tune(), min_n = tune()) %>% set_engine('rpart') %>% set_mode('classification')dt_tune_model
Decision Tree Model Specification (classification)
Main Arguments:
cost_complexity = tune()
tree_depth = tune()
min_n = tune()
Computational engine: rpart
workflow 객체는 쉽게 업데이트 가능
leads_wkflleads_wkfl을 update_model()에 전달하고 튜닝 파라미터가 있는 새 결정 트리 모델 제공leads_tune_wkfl <- leads_wkfl %>%update_model(dt_tune_model)leads_tune_wkfl
== Workflow ===============
Preprocessor: Recipe
Model: decision_tree()
-- Preprocessor -----------
3 Recipe Steps
* step_corr()
* step_normalize()
* step_dummy()
-- Model ------------------
Decision Tree Model Specification (classification)
Main Arguments: cost_complexity = tune()
tree_depth = tune()
min_n = tune()
Computational engine: rpart
하이퍼파라미터 튜닝에서 가장 일반적인 방법
| cost_complexity | tree_depth | min_n |
|---|---|---|
| 0.001 | 20 | 35 |
| 0.001 | 20 | 15 |
| 0.001 | 35 | 35 |
| 0.001 | 35 | 15 |
| 0.2 | 20 | 35 |
| ... | ... | ... |
dials 패키지의 parameters() 함수
parsnip 모델 객체를 인수로 받음tune() 함수로 레이블된 하이퍼파라미터가 있으면 티블로 반환dials 패키지에서 튜닝 그리드 생성에 사용parameters(dt_tune_model)
Collection of 3 parameters for tuning
identifier type object
cost_complexity cost_complexity nparam[+]
tree_depth tree_depth nparam[+]
min_n min_n nparam[+]
무작위 조합 생성
grid_random() 함수
parameters() 함수의 결과size는 생성할 무작위 조합의 수 설정grid_random() 전에 set.seed() 실행set.seed(214) grid_random(parameters(dt_tune_model),size = 5)
# A tibble: 5 x 3
cost_complexity tree_depth min_n
<dbl> <int> <int>
1 0.0000000758 14 39
2 0.0243 5 34
3 0.00000443 11 8
4 0.000000600 3 5
5 0.00380 5 36
하이퍼파라미터 튜닝의 첫 번째 단계
dt_grid에는 하이퍼파라미터 값의 무작위 조합 5개 포함set.seed(214) dt_grid <- grid_random(parameters(dt_tune_model), size = 5)dt_grid
# A tibble: 5 x 3
cost_complexity tree_depth min_n
<dbl> <int> <int>
1 0.0000000758 14 39
2 0.0243 5 34
3 0.00000443 11 8
4 0.000000600 3 5
5 0.00380 5 36
tune_grid() 함수로 하이퍼파라미터 튜닝 수행
다음 인수를 받음:
workflow 또는 parsnip 모델resamplesgridmetrics 함수결과 티블 반환
.metricsdt_tuning <- leads_tune_wkfl %>%tune_grid(resamples = leads_folds,grid = dt_grid,metrics = leads_metrics)
dt_tuning
# Tuning results
# 10-fold cross-validation using stratification
# A tibble: 10 x 4
splits id .metrics ..
<list> <chr> <list> ..
<split [896/100]> Fold01 <tibble [15 x 7]> ..
................ ...... ............... ..
<split [897/99]> Fold09 <tibble [15 x 7]> ..
<split [897/99]> Fold10 <tibble [15 x 7]> ..
collect_metrics() 함수는 기본적으로 요약된 결과 제공
dt_tuning %>%
collect_metrics()
# A tibble: 15 x 9
cost_complexity tree_depth min_n .metric .estimator mean n std_err .config
<dbl> <int> <int> <chr> <chr> <dbl> <int> <dbl> <chr>
1 0.0000000758 14 39 roc_auc binary 0.827 10 0.0147 Model1
2 0.0000000758 14 39 sens binary 0.728 10 0.0277 Model1
3 0.0000000758 14 39 spec binary 0.865 10 0.0156 Model1
4 0.0243 5 34 roc_auc binary 0.823 10 0.0147 Model2
. ...... .. .. .... ...... ..... .. ..... ......
14 0.00380 5 36 sens binary 0.747 10 0.0209 Model5
15 0.00380 5 36 spec binary 0.858 10 0.0161 Model5
R에서 tidymodels로 모델링하기