繁体   English   中英

R创建2 * 2混淆矩阵的通用解决方案

[英]R Generic solution to create 2*2 confusion matrix

我的问题是与此相关的一个上与制造中的R混淆矩阵table()函数。 我正在寻找不使用软件包(例如插入符号)的解决方案。

假设这些是我们在二进制分类问题中的predictionslabels

predictions <- c(0.61, 0.36, 0.43, 0.14, 0.38, 0.24, 0.97, 0.89, 0.78, 0.86, 0.15,  0.52, 0.74, 0.24)
labels      <- c(1,    1,    1,    0,    0,     1,    1,    1,    0,     1,    0,    0,    1,    0)

对于这些值,下面的解决方案可以很好地为阈值= 0.5创建2 * 2混淆矩阵:

# Confusion matrix for threshold = 0.5
conf_matrix <- as.matrix(table(predictions>0.5,labels))
  conf_matrix
     labels
       0 1
 FALSE 4 3
 TRUE  2 5

但是,如果我选择小于min(predictions)或大于max(predictions)任何值,则不会获得2 * 2矩阵,因为数据不会出现FALSE或TRUE,例如:

conf_matrix <- as.matrix(table(predictions>0.05,labels))
  conf_matrix
     labels
       0 1
  TRUE 6 8

我需要一种为0到1之间的所有可能阈值(决策边界)持续生成2 * 2混淆矩阵的方法,因为我将其用作优化的输入。 有没有一种方法可以调整table函数,使其在此处始终返回2 * 2矩阵?

您可以将阈值预测作为因子变量来实现:

(conf_matrix <- as.matrix(table(factor(predictions>0.05, levels=c(F, T)), labels)))
#        labels
#         0 1
#   FALSE 0 0
#   TRUE  6 8

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM