트리를 키우는 방법

R로 배우는 트리 기반 Machine Learning

Sandro Raabe

Data Scientist

당뇨병 데이터셋

head(diabetes)
# A tibble: 6 x 9
  outcome pregnancies glucose blood_pressure skin_thickness insulin   bmi    age
  <fct>         <int>   <int>          <int>          <int>   <int> <dbl>  <int>
1 yes               6     148             72             35       0  33.6     50
2 no                1      85             66             29       0  26.6     31
3 yes               8     183             64              0       0  23.3     32
R로 배우는 트리 기반 Machine Learning

전체 데이터 사용 시 문제점

  • 전체 데이터를 학습에 사용하면 모델 테스트에 쓸 데이터가 없습니다

model_flow_1

R로 배우는 트리 기반 Machine Learning

데이터 분할

데이터 분할

모델과 평가

R로 배우는 트리 기반 Machine Learning

분할 방법

분할 방법

분할_방법2

분할_방법3

R로 배우는 트리 기반 Machine Learning

initial_split() 함수

  • 데이터를 무작위로 단일 학습/단일 테스트 세트로 분할
# 비율로 데이터 분할(기본값: 0.75)
diabetes_split <- initial_split(diabetes, prop = 0.9)
diabetes_split
<Analysis/Assess/Total>
<692/76/768>
1 rsample 패키지 제공
R로 배우는 트리 기반 Machine Learning

training()과 testing() 함수

  • 데이터 분할에서 학습/테스트 세트 추출
diabetes_train <- training(diabetes_split)

diabetes_test <- testing(diabetes_split)
  • 검증:
    nrow(diabetes_train)/nrow(diabetes)
    
[1] 0.9007812
1 rsample 제공
R로 배우는 트리 기반 Machine Learning

클래스 불균형 피하기

# 학습 데이터에서 'yes'와 'no' 개수
counts_train <- table(diabetes_train$outcome)
counts_train
 no yes 
490 86
# 학습 데이터에서 'yes' 비율
prop_yes_train <- counts_train["yes"]/
                  sum(counts_train)
prop_yes_train
0.15
# 테스트 데이터에서 'yes'와 'no' 개수
counts_test <- table(diabetes_test$outcome)
counts_test
 no yes 
 28  48
# 테스트 데이터에서 'yes' 비율
prop_yes_test <- counts_test["yes"]/
                  sum(counts_test)
prop_yes_test
0.63
R로 배우는 트리 기반 Machine Learning

해결: 분포 유사성 강제

initial_split(diabetes, 
              prop = 0.9, 
              strata = outcome)
  • outcome 분포를 유사하게 유지하며 무작위 분할 보장
R로 배우는 트리 기반 Machine Learning

분할해 봅시다!

R로 배우는 트리 기반 Machine Learning

Preparing Video For Download...