R에서 tidymodels로 모델링하기
David Svancer
Data Scientist
last_fit() 함수
fit() 사용과 유사하게 초기 단계:
rsample로 데이터 분할 객체 생성parsnip으로 모델 지정leads_split <- initial_split(leads_df, strata = purchased)logistic_model <- logistic_reg() %>% set_engine('glm') %>% set_mode('classification')
last_fit() 함수에 필요한 것
parsnip 모델 객체
collect_metrics()는 테스트 데이터셋으로 메트릭을 계산합니다
logistic_last_fit <- logistic_model %>% last_fit(purchased ~ total_visits + total_time, split = leads_split)logistic_last_fit %>% collect_metrics()
# A tibble: 2 x 3
.metric .estimator .estimate
<chr> <chr> <dbl>
1 accuracy binary 0.759
2 roc_auc binary 0.763
collect_predictions()
yardstick 함수에 필요한 열을 담은 tibble 생성last_fit_results <- logistic_last_fit %>%
collect_predictions()
last_fit_results
# A tibble: 332 x 6
id .pred_yes .pred_no .row .pred_class purchased
<chr> <dbl> <dbl> <int> <fct> <fct>
1 train/test split 0.134 0.866 2 no no
2 train/test split 0.729 0.271 17 yes yes
3 train/test split 0.133 0.867 21 no no
4 train/test split 0.0916 0.908 22 no no
5 train/test split 0.598 0.402 24 yes yes
# ... with 327 more rows
metric_set() 함수
accuracy(), sens(), spec()truth와 estimate 인수가 필요roc_auc()truth와 예측 확률 열이 필요
custom_metrics()에는 세 모두가 필요하며 마지막 인수는 .pred_yes 입니다
custom_metrics <- metric_set(accuracy, sens,
spec, roc_auc)
custom_metrics(last_fit_results,
truth = purchased,
estimate = .pred_class,
.pred_yes)
# A tibble: 4 x 3
.metric .estimator .estimate
<chr> <chr> <dbl>
1 accuracy binary 0.759
2 sens binary 0.617
3 spec binary 0.840
4 roc_auc binary 0.763
R에서 tidymodels로 모델링하기