모델의 특성 수 줄이기

R로 배우는 Feature Engineering

Jorge Zazueta

Research Professor and Head of the Modeling Group at the School of Economics, UASLP

특성 수를 줄이는 이유

관련 없거나 정보가 적은 변수를 제거하면 다음과 같은 이점이 있습니다.

  • 편향을 크게 늘리지 않고 분산 감소
  • 홀드아웃 성능 향상
  • 계산 시간 단축
  • 모델 복잡도 감소
  • 해석 가능성 향상
R로 배우는 Feature Engineering

변수 중요도로 거르기

모든 특성으로 모델 적합

lr_recipe_full <-
  recipe(Loan_Status ~., data = train) %>%
  update_role(Loan_ID, new_role = "ID")

lr_workflow_full <- 
  workflow() %>%
  add_model(lr_model) %>%
  add_recipe(lr_recipe_full)

lr_fit_full <- 
  lr_workflow_full %>%
  fit(data = train)

변수 중요도 vip 그래프화

lr_fit_full %>%
  extract_fit_parsnip() %>%
  vip(aesthetics = list(fill = "steelblue"))

변수 중요도 변수 중요도 막대그래프.

R로 배우는 Feature Engineering

수식 문법으로 축소 모델 구축

기본 R 수식 문법으로 특성을 직접 지정할 수 있습니다.

# 레시피 생성
recipe_formula <- 
  recipe(Loan_Status ~ Credit_History + Property_Area + 
           LoanAmount, data = train)

# 모델과 묶기
workflow_formula <- # 모델과 묶기
  workflow() %>% add_model(lr_model) %>%
  add_recipe(recipe_formula)
R로 배우는 Feature Engineering

특성 벡터로 축소 모델 구축

특성 벡터를 사용해 학습 전 특성을 선택할 수 있습니다.

# 특성 벡터
features <- c("Credit_History", "Property_Area", "LoanAmount", "Loan_Status") 

# 학습/테스트 데이터
train_features <- train %>% select(all_of(features))
test_features <- test %>% select(all_of(features))

# 레시피 생성 및 모델과 묶기
recipe_features <- recipe(Loan_Status ~., data = train_features)
workflow_features <- workflow() %>% add_model(lr_model) %>%
  add_recipe(recipe_features) 
R로 배우는 Feature Engineering

보강 객체 만들기

두 접근법의 보강(augmented) 객체

lr_aug_formula <-
  workflow_formula %>%
  fit(data = train) %>%
  augment(new_data = test)
lr_aug_features <-
  workflow_features %>%
  fit(data = train_features) %>%
  augment(new_data = test_features)

두 방법은 동일한 결과를 반환

all_equal(lr_aug_features, 
lr_aug_formula %>%
select(all_of(features),
starts_with(".pred")))
[1] TRUE
R로 배우는 Feature Engineering

전체 모델과 축소 모델 비교

모든 특성 사용

lr_fit_full <- # 워크플로 적합
  lr_workflow_full %>%
  fit(data = train)
lr_aug_full <- # 보강
  lr_fit_full %>%
  augment(test)
lr_aug_full %>% # 평가
  class_evaluate(truth = Loan_Status, 
                 estimate = .pred_class,
                 .pred_Y)
# A tibble: 2 × 3
  .metric  .estimator .estimate
  <chr>    <chr>          <dbl>
1 accuracy binary         0.842
2 roc_auc  binary         0.744

상위 3개 특성 사용*

lr_fit_formula <- # 워크플로 적합
  workflow_formula %>%
  fit(train)
lr_aug_formula <- # 보강
  lr_fit_formula %>%
  augment(new_data = test)
lr_aug_formula %>% # 평가
  class_evaluate(truth = Loan_Status, 
                 estimate = .pred_class,
                 .pred_Y)
# A tibble: 2 × 3
  .metric  .estimator .estimate
  <chr>    <chr>          <dbl>
1 accuracy binary         0.842
2 roc_auc  binary         0.733
R로 배우는 Feature Engineering

Ayo berlatih!

R로 배우는 Feature Engineering

Preparing Video For Download...