Mô hình hóa với tidymodels trong R
David Svancer
Data Scientist
Biến đầu ra hai mức
purchasedLớp âm (Negative class)
Trong tidymodels, biến đầu ra phải là factor
levels()leads_df
# A tibble: 1,328 x 7
purchased total_visits ... us_location
<fct> <dbl> ... <fct>
1 yes 7 ... west
2 no 8 ... west
3 no 5 ... southeast
# ... with 1,325 more rows
levels(leads_df[['purchased']])
[1] "yes" "no"
Ma trận đếm mọi kết hợp của giá trị thực tế và dự đoán
Dự đoán đúng
Lỗi phân loại
Tạo ma trận nhầm lẫn và chỉ số độ phù hợp bằng yardstick
purchased.pred_class.pred_yes.pred_noleads_results
# A tibble: 332 x 4
purchased .pred_class .pred_yes .pred_no
<fct> <fct> <dbl> <dbl>
1 no no 0.134 0.866
2 yes yes 0.729 0.271
3 no no 0.133 0.867
4 no no 0.0916 0.908
5 yes yes 0.598 0.402
6 no no 0.128 0.872
7 yes no 0.112 0.888
8 no no 0.169 0.831
9 no no 0.158 0.842
10 yes yes 0.520 0.480
# ... with 322 more rows
Hàm conf_mat()
truth - cột nhãn thựcestimate - cột nhãn dự đoánHồi quy logistic trên leads_df
conf_mat(leads_results,truth = purchased,estimate = .pred_class)
Truth
Prediction yes no
yes 74 34
no 46 178
Hàm accuracy()
conf_mat()
$$\frac{TP + TN}{TP + TN + FP + FN}$$
yardstick luôn trả về tibble.metric - loại chỉ số.estimate - giá trị tính đượcaccuracy(leads_results,
truth = purchased,
estimate = .pred_class)
# A tibble: 1 x 3
.metric .estimator .estimate
<chr> <chr> <dbl>
1 accuracy binary 0.759
Nhiều trường hợp accuracy không phải chỉ số tốt nhất
leads_df
Độ nhạy (Sensitivity)
Tỷ lệ các trường hợp dương tính được phân loại đúng
Hàm sens()
conf_mat() và accuracy().estimatesens(leads_results,
truth = purchased,
estimate = .pred_class)
# A tibble: 1 x 3
.metric .estimator .estimate
<chr> <chr> <dbl>
1 sens binary 0.617
Độ đặc hiệu (Specificity) là tỷ lệ các trường hợp âm tính được phân loại đúng
1 - Độ đặc hiệu
Hàm spec()
sens().estimatespec(leads_results,
truth = purchased,
estimate = .pred_class)
# A tibble: 1 x 3
.metric .estimator .estimate
<chr> <chr> <dbl>
1 spec binary 0.840
Bộ chỉ số do người dùng định nghĩa
metric_set()yardstick đã chọnyardstick vào metric_set()custom_metrics <-
metric_set(accuracy, sens, spec)
custom_metrics(leads_results,
truth = purchased,
estimate = .pred_class)
# A tibble: 3 x 3
.metric .estimator .estimate
<chr> <chr> <dbl>
1 accuracy binary 0.759
2 sens binary 0.617
3 spec binary 0.840
Chỉ số phân loại nhị phân
Nhiều chỉ số cho phân loại nhị phân
accuracy(), kap(), sens(), spec(), ppv(), npv(), mcc(), j_index(), bal_accuracy(), detection_prevalence(), precision(), recall(), f_meas()Truyền kết quả conf_mat() vào summary() để tính tất cả
conf_mat(leads_results, truth = purchased,
estimate = .pred_class) %>%
summary()
# A tibble: 13 x 3
.metric .estimator .estimate
<chr> <chr> <dbl>
1 accuracy binary 0.759
2 kap binary 0.466
3 sens binary 0.617
4 spec binary 0.840
5 ppv binary 0.685
6 npv binary 0.795
7 mcc binary 0.468
8 j_index binary 0.456
9 bal_accuracy binary 0.728
10 detection_prevalence binary 0.325
11 precision binary 0.685
12 recall binary 0.617
13 f_meas binary 0.649
Mô hình hóa với tidymodels trong R