予測と評価

Rで学ぶTree-Based ModelsによるMachine Learning

Sandro Raabe

Data Scientist

新規データへの予測

一般的な呼び出し:
predict(model, new_data, type)
引数:
  1. 学習済みモデル
  2. 予測対象のデータ
  3. 予測タイプ: ラベルまたは確率
Rで学ぶTree-Based ModelsによるMachine Learning

新規データへの予測

predict(model, new_data = test_data, 
               type = "class")
  .pred_class
  <fct>      
1 no         
2 no         
3 yes        
4 no
predict(model, new_data = test_data, 
               type = "prob")
     .pred_no  .pred_yes
     <dbl>     <dbl>
1    0.866     0.134
2    0.956     0.044
3    0.672     0.328
4    0.877     0.123
Rで学ぶTree-Based ModelsによるMachine Learning

混同行列

混同行列_1

  • モデルの取り違えを可視化
Rで学ぶTree-Based ModelsによるMachine Learning

混同行列

混同行列_2

Rで学ぶTree-Based ModelsによるMachine Learning

混同行列

混同行列_3

Rで学ぶTree-Based ModelsによるMachine Learning

混同行列

混同行列_4

  • 対角: 正解
  • 非対角: 誤り
Rで学ぶTree-Based ModelsによるMachine Learning

混同行列

 

  • TP: 予測yes, 真値yes
  • TN: 予測no, 真値no
  • FP: 予測yes, 真値no
  • FN: 予測no, 真値yes

混同行列_4

Rで学ぶTree-Based ModelsによるMachine Learning

混同行列を作成

# 予測と真値を結合
pred_combined <- predictions %>% 
   mutate(true_class = test_data$outcome)

pred_combined
  .pred_class  true_class
  <fct>        <fct>     
1 no           no        
2 no           yes       
3 no           no        
4 yes          yes
# 混同行列を計算
conf_mat(data = pred_combined,

estimate = .pred_class,
truth = true_class)
             Truth
Prediction    no   yes
        no   116    31
       yes    12    40
Rで学ぶTree-Based ModelsによるMachine Learning

正解率(Accuracy)

  $$\text{accuracy} = \frac{\text{正解数}}{\text{予測総数}}$$

  • 関数名: accuracy()
  • conf_mat()と同じ引数
    • dataestimatetruth
    • yardstickで共通の構造
accuracy(pred_combined,
         estimate = .pred_class,
         truth = true_class)
  .metric     .estimate
  <chr>           <dbl>
1 accuracy        0.708
Rで学ぶTree-Based ModelsによるMachine Learning

評価してみましょう!

Rで学ぶTree-Based ModelsによるMachine Learning

Preparing Video For Download...