R로 배우는 트리 기반 Machine Learning
Sandro Raabe
Data Scientist
parsnip 결정트리의 하이퍼파라미터:
min_n: 노드 분할에 필요한 최소 샘플 수tree_depth: 허용되는 최대 트리 깊이cost_complexity: 트리 복잡도에 대한 패널티parsnip의 기본값:
decision_tree(min_n = 20, tree_depth = 30, cost_complexity = 0.01)
튜닝의 목표는 하이퍼파라미터의 최적 조합을 찾는 것입니다.




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()으로 튜닝 대상 파라미터 표시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
parameters()levels: 각 하이퍼파라미터의 그리드 포인트 수
사용법 및 인자:
metric_set()으로 감싼 메트릭 목록tune_results <- tune_grid(spec_untuned,outcome ~ .,resamples = my_folds,grid = tree_grid,metrics = metric_set(accuracy))
autoplot(tune_results)

# 가장 성능이 좋은 파라미터 선택 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
# 사양에 반영 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
R로 배우는 트리 기반 Machine Learning