混淆矩陣

使用 R 的 caret 進行 Machine Learning

Zach Mayer

Data Scientist at DataRobot and co-author of caret

混淆矩陣

預測對上參考的混淆矩陣。當預測與參考皆為「yes」時是真陽性;皆為「no」時是真陰性;預測為「yes」但參考為「no」是偽陽性;預測為「no」但參考為「yes」是偽陰性。

使用 R 的 caret 進行 Machine Learning

混淆矩陣

# 訓練模型
model <- glm(Class ~ ., family = binomial(link = "logit"), train)
p <- predict(model, test, type = "response")
summary(p)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.0000  0.0000  0.9885  0.5296  1.0000  1.0000 
# 將機率轉為類別並查看其頻數
p_class <- ifelse(p > 0.50, "M", "R")
table(p_class)
p_class
 M  R 
44 39
使用 R 的 caret 進行 Machine Learning

混淆矩陣

  • 建立二向度列聯表
  • 比較預測與實際類別
# 建立簡單的二向度列聯表
table(p_class, test[["Class"]])
p_class  M  R
      M 13 31
      R 30  9
使用 R 的 caret 進行 Machine Learning

混淆矩陣

# 使用 caret 的輔助函式計算更多統計量
confusionMatrix(p_class, test[["Class"]])
         Reference
Prediction  M  R
         M 13 31
         R 30  9

               Accuracy : 0.2651          
                 95% CI : (0.1742, 0.3734)
    No Information Rate : 0.5181          
    P-Value [Acc > NIR] : 1               

                  Kappa : -0.4731         
 Mcnemar's Test P-Value : 1               

            Sensitivity : 0.3023          
            Specificity : 0.2250          
         Pos Pred Value : 0.2955          
         Neg Pred Value : 0.2308
使用 R 的 caret 進行 Machine Learning

一起來練習吧!

使用 R 的 caret 進行 Machine Learning

Preparing Video For Download...