混淆矩阵

在 R 中使用 caret 的机器学习

Zach Mayer

Data Scientist at DataRobot and co-author of caret

混淆矩阵

预测 vs. 真实的混淆矩阵。预测与真实都为"yes"为真正类(TP)。都为"no"为真负类(TN)。预测为"yes"而真实为"no"为假正类(FP)。预测为"no"而真实为"yes"为假负类(FN)。

在 R 中使用 caret 的机器学习

混淆矩阵

# 拟合模型
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 的机器学习

混淆矩阵

  • 构建二维频数表
  • 对比预测类别与真实类别
# 简单双向频数表
table(p_class, test[["Class"]])
p_class  M  R
      M 13 31
      R 30  9
在 R 中使用 caret 的机器学习

混淆矩阵

# 使用 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 的机器学习

让我们练习吧!

在 R 中使用 caret 的机器学习

Preparing Video For Download...