简体   繁体   English

在Python中计算confusion_matrix

[英]counting confusion_matrix in python

I wan to calculate calculate True_Positive, False_Positive,False_Negative, True_Negative for three categories. 我想计算三个类别的True_Positive, False_Positive,False_Negative, True_Negative I used to have two classes Cat Dog and this is the way i used to calculate my confusion_matrix 我以前有两个班级的Cat Dog ,这是我用来计算confusion_matrix的方式

    Y_pred has either a cat or dog 
    y_true has either a cat or dog  
    confusion_matrix_output =confusion_matrix(y_true, y_pred) 
    True_Positive = confusion_matrix_output[0][0]
    False_Positive = confusion_matrix_output[0][1]
    False_Negative = confusion_matrix_output[1][0]
    True_Negative = confusion_matrix_output[1][1]

Now i have three classes 'Cat' 'Dog' 'rabbit' 现在我有三个班级'猫''狗''兔子'

Y_pred has Cat Dog rabbit
y_true has Cat Dog rabbit

How to calculate True_Positive, False_Positive,False_Negative, True_Negative ??? 如何计算True_Positive,False_Positive,False_Negative,True_Negative ???

Now you have three classes, so it's not just positives and negatives anymore. 现在您有了三个类别,因此不再只是正面和负面。 You have to look at: Cat predicted as a cat, Dog predicted as Dog, Rabbit predicted as Rabbit, Dog predicted as cat, Cat predicted as Dog, and so on. 您必须查看:将Cat预测为猫,将Dog预测为Dog,将Rabbit预测为Rabbit,将Dog预测为cat,将Cat预测为Dog,等等。 You'll have 3 by 3 confusion matrix for this situation. 对于这种情况,您将有3 x 3的混淆矩阵。 Confusion matrix size is n by n, where n is the number of classes 混淆矩阵的大小为n x n,其中n为类别数

sklearn.metrics.confusion_matrix abstracts away all that and creates an n by n matrix for you. sklearn.metrics.confusion_matrix将所有sklearn.metrics.confusion_matrix抽象化,并为您创建一个n by n矩阵。 Try this: 尝试这个:

from sklearn.metrics import confusion_matrix
confusion_matrix_output =confusion_matrix(y_true, y_pred) 
Cat_P_Cat = confusion_matrix_output[0][0]

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

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