R での tidymodels によるモデリング
David Svancer
Data Scientist
決定木は予測子空間を長方形領域に分割する
再帰的二分割

決定木は予測子空間を長方形領域に分割する
再帰的二分割

決定木は予測子空間を長方形領域に分割する
再帰的二分割

決定木は予測子空間を長方形領域に分割する
再帰的二分割

決定木は予測子空間を長方形領域に分割する
再帰的二分割
明確な長方形領域を生成


内部ノードは破線、終端ノードは強調表示された長方形領域

parsnipでのモデル指定
decision_tree()parsnipの決定木モデル用インターフェース'rpart''classification'または'regression''classification'が必要dt_model <- decision_tree() %>%set_engine('rpart') %>%set_mode('classification')
リードスコアリング用データの変換
recipeオブジェクトに定義管理するRオブジェクトは2つ
parsnipモデルとrecipeの指定leads_recipe <- recipe(purchased ~ ., data = leads_training) %>%step_corr(all_numeric(), threshold = 0.9) %>% step_normalize(all_numeric()) %>% step_dummy(all_nominal(), -all_outcomes())
leads_recipe
Data Recipe
Inputs:
role #variables
outcome 1
predictor 6
Operations:
Correlation filter on all_numeric()
Centering and scaling for all_numeric()
Dummy variables from all_nominal(), -all_outcomes()
workflowsパッケージはモデリング工程を簡素化するためのもの
parsnipモデルとrecipeを1つのworkflowに結合
workflow()で初期化
add_model()でモデルを追加add_recipe()でrecipeを追加leads_wkfl <- workflow() %>%add_model(dt_model) %>%add_recipe(leads_recipe)leads_wkfl
== Workflow =====================
Preprocessor: Recipe
Model: decision_tree()
-- Preprocessor -----------------
3 Recipe Steps
* step_corr()
* step_normalize()
* step_dummy()
-- Model --------------------------
Decision Tree Model Specification (classification)
Computational engine: rpart
workflowオブジェクトの学習
last_fit()にworkflowとデータ分割オブジェクトを渡すcollect_metrics()で確認舞台裏
recipeを学習し適用leads_wkfl_fit <- leads_wkfl %>% last_fit(split = leads_split)leads_wkfl_fit %>% collect_metrics()
# A tibble: 2 x 3
.metric .estimator .estimate
<chr> <chr> <dbl>
1 accuracy binary 0.771
2 roc_auc binary 0.775
last_fit()で学習したworkflowはcollect_predictions()に渡せる
yardstick関数でカスタム指標を評価可能leads_wkfl_preds <- leads_wkfl_fit %>% collect_predictions()leads_wkfl_preds
# A tibble: 332 x 6
id .pred_yes .pred_no .row .pred_class purchased
<chr> <dbl> <dbl> <int> <fct> <fct>
train/test split 0.120 0.880 2 no no
train/test split 0.755 0.245 17 yes yes
train/test split 0.120 0.880 21 no no
train/test split 0.120 0.880 22 no no
train/test split 0.755 0.245 24 yes yes
# ... with 327 more rows
metric_set()でカスタム指標セットを作成
予測データをleads_metrics()に渡して指標を計算
leads_metrics <- metric_set(roc_auc, sens, spec)leads_wkfl_preds %>% leads_metrics(truth = purchased, estimate = .pred_class, .pred_yes)
# A tibble: 3 x 3
.metric .estimator .estimate
<chr> <chr> <dbl>
1 sens binary 0.75
2 spec binary 0.783
3 roc_auc binary 0.775
銀行の個人向けローンの財務データ
loan_default
loans_df
# A tibble: 872 x 8
loan_default loan_purpose missed_payment_2_yr loan_amount interest_rate installment annual_income debt_to_income
<fct> <fct> <fct> <int> <dbl> <dbl> <dbl> <dbl>
no debt_consolidation no 25000 5.47 855. 62823 39.4
yes medical no 10000 10.2 364. 40000 24.1
no small_business no 13000 6.22 442. 65000 14.0
no small_business no 36000 5.97 1152. 125000 8.09
yes small_business yes 12000 11.8 308. 65000 20.1
# ... with 867 more rows
R での tidymodels によるモデリング