简体   繁体   English

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

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

My question is related to this one on producing a confusion matrix in R with the table() function. 我的问题是与此相关的一个上与制造中的R混淆矩阵table()函数。 I am looking for a solution without using a package (eg caret). 我正在寻找不使用软件包(例如插入符号)的解决方案。

Let's say these are our predictions and labels in a binary classification problem: 假设这些是我们在二进制分类问题中的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)

For these values, the solution below works well to create a 2*2 confusion matrix for, let's say, threshold = 0.5: 对于这些值,下面的解决方案可以很好地为阈值= 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

However, I do not get a 2*2 matrix if I select any value that is smaller than min(predictions) or larger than max(predictions) , since the data won't have either a FALSE or TRUE occurrence eg: 但是,如果我选择小于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

I need a method that consistently produces a 2*2 confusion matrix for all possible thresholds (decision boundaries) between 0 and 1, as I use this as an input in an optimisation. 我需要一种为0到1之间的所有可能阈值(决策边界)持续生成2 * 2混淆矩阵的方法,因为我将其用作优化的输入。 Is there a way I can tweak the table function so it always returns a 2*2 matrix here? 有没有一种方法可以调整table函数,使其在此处始终返回2 * 2矩阵?

You can make your thresholded prediction a factor variable to achieve this: 您可以将阈值预测作为因子变量来实现:

(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