Mô hình hóa với tidymodels trong R
David Svancer
Data Scientist
Tạo tập huấn luyện và kiểm tra là bước đầu của quá trình mô hình hóa
Hạn chế
Kỹ thuật lấy mẫu lại để khám phá hiệu năng mô hình

Kỹ thuật lấy mẫu lại để khám phá hiệu năng mô hình

Thực hiện 5-fold cross validation

Thực hiện 5-fold cross validation

Thực hiện 5-fold cross validation

Thực hiện 5-fold cross validation
Tổng cộng có năm ước lượng hiệu năng mô hình

Hàm vfold_cv()
vstrataset.seed() trước vfold_cv() để tái lập kết quảsplitsset.seed(214) leads_folds <- vfold_cv(leads_training,v = 10,strata = purchased)leads_folds
# 10-fold cross-validation using stratification
# A tibble: 10 x 2
splits id
<list> <chr>
1 <split [896/100]> Fold01
2 <split [896/100]> Fold02
3 <split [896/100]> Fold03
. ................ ......
9 <split [897/99]> Fold09
10 <split [897/99]> Fold10
Hàm fit_resamples()
parsnip hoặc đối tượng workflow resamplesmetrics
Mỗi metric được ước lượng 10 lần
meanleads_rs_fit <- leads_wkfl %>%fit_resamples(resamples = leads_folds,metrics = leads_metrics)leads_rs_fit %>% collect_metrics()
# A tibble: 3 x 5
.metric .estimator mean n std_err
<chr> <chr> <dbl> <int> <dbl>
1 roc_auc binary 0.823 10 0.0147
2 sens binary 0.786 10 0.0203
3 spec binary 0.855 10 0.0159
Hàm collect_metrics()
summarize = FALSE để lấy mọi ước lượng metric cho từng fold cross validation.metric xác định loại metric.estimate là giá trị ước lượng cho từng foldrs_metrics <- leads_rs_fit %>% collect_metrics(summarize = FALSE)rs_metrics
# A tibble: 30 x 4
id .metric .estimator .estimate
<chr> <chr> <chr> <dbl>
1 Fold01 sens binary 0.861
2 Fold01 spec binary 0.891
3 Fold01 roc_auc binary 0.885
4 Fold02 sens binary 0.778
5 Fold02 spec binary 0.969
6 Fold02 roc_auc binary 0.885
# ... with 24 more rows
Hàm collect_metrics() trả về một tibble
dplyrrs_metrics.metricsummarize()rs_metrics %>%group_by(.metric) %>%summarize(min = min(.estimate), median = median(.estimate), max = max(.estimate), mean = mean(.estimate), sd = sd(.estimate))
# A tibble: 3 x 6
.metric min median max mean sd
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 roc_auc 0.758 0.806 0.885 0.823 0.0466
2 sens 0.667 0.792 0.861 0.786 0.0642
3 spec 0.810 0.843 0.969 0.855 0.0502
Các mô hình huấn luyện bằng fit_resamples() không thể dự đoán cho nguồn dữ liệu mới
predict() không chấp nhận đối tượng resampleMục đích của fit_resample()
predict(leads_rs_fit,
new_data = leads_test)
Error in UseMethod("predict") :
no applicable method for 'predict' applied to
an object of class
"c('resample_results',
'tune_results',
'tbl_df',
'tbl', 'data.frame')"
Mô hình hóa với tidymodels trong R