tidymodels로 모델 구축과 평가

R에서의 차원 축소

Matt Pickard

Owner, Pickard Predictives, LLC

모델 적합 프로세스

모델 적합의 첫 단계는 데이터 분할입니다

R에서의 차원 축소

모델 적합 프로세스

모델 적합의 두 번째 단계는 데이터 준비입니다

R에서의 차원 축소

모델 적합 프로세스

모델 적합의 세 번째 단계는 모델을 학습시키는 것입니다

R에서의 차원 축소

모델 적합 프로세스

모델 적합의 네 번째 단계는 모델 평가입니다

R에서의 차원 축소

tidymodels로 모델 적합

tidymodels는 데이터를 학습/테스트 세트로 분할하는 함수가 있습니다

R에서의 차원 축소

tidymodels로 모델 적합

tidymodels의 recipe는 데이터 전처리 단계를 만드는 함수가 있습니다

R에서의 차원 축소

tidymodels로 모델 적합

tidymodels는 워크플로에서 다양한 모델을 적합하는 함수가 있습니다

R에서의 차원 축소

학습/테스트 세트 분할

split <- initial_split(credit_df, prop = 0.8, strata = credit_score)


train <- split %>% training()
test <- split %>% testing()
R에서의 차원 축소

레시피와 모델 생성

feature_selection_recipe <- 
  recipe(credit_score ~ ., data = train) %>%

step_filter_missing(all_predictors(), threshold = 0.5) %>%
step_scale(all_numeric_predictors()) %>%
step_nzv(all_predictors()) %>%
prep()
lr_model <- logistic_reg() %>%

set_engine("glm")
R에서의 차원 축소

워크플로 생성 및 적합

credit_wflow <- workflow() %>%

add_recipe(feature_selection_recipe) %>%
add_model(lr_model)
credit_fit <- credit_wflow %>% fit(data = train)
R에서의 차원 축소

모델 평가

# Predict test data
credit_pred_df <- predict(credit_fit, test) %>% 
  bind_cols(test %>% select(credit_score))


# Evaluate F score f_meas(credit_pred_df, credit_score, .pred_class)
# A tibble: 1 × 3
  .metric .estimator .estimate
  <chr>   <chr>          <dbl>
1 f_meas  macro          0.519
R에서의 차원 축소

tidy()로 레시피 탐색

tidy(feature_selection_recipe, number = 1)
# A tibble: 2 × 2
  terms            id                  
  <chr>            <chr>               
1 age              filter_missing_gVVfc
2 outstanding_debt filter_missing_gVVfc
R에서의 차원 축소

tidy()로 모델 탐색

# Display model estimates
tidy(credit_fit)
# A tibble: 44 × 5
   term                estimate std.error statistic p.value
   <chr>                  <dbl>     <dbl>     <dbl>   <dbl>
 1 (Intercept)           2.88       0.918    3.13   0.00173
 2 monthAugust          -0.449      0.236   -1.91   0.0565 
 3 monthFebruary        17.7      677.       0.0262 0.979  
 4 monthJanuary         17.7      661.       0.0268 0.979  
 ...                    ...       ...        ...    ... 
R에서의 차원 축소

Ayo berlatih!

R에서의 차원 축소

Preparing Video For Download...