python中多类数据的真阳性率和假阳性率(TPR,FPR) [英] True Positive Rate and False Positive Rate (TPR, FPR) for Multi-Class Data in python

查看:59
本文介绍了python中多类数据的真阳性率和假阳性率(TPR,FPR)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算多类分类问题的真假阳性率?说,

How do you compute the true- and false- positive rates of a multi-class classification problem? Say,

y_true = [1, -1,  0,  0,  1, -1,  1,  0, -1,  0,  1, -1,  1,  0,  0, -1,  0]
y_prediction = [-1, -1,  1,  0,  0,  0,  0, -1,  1, -1,  1,  1,  0,  0,  1,  1, -1]

混淆矩阵由 metrics.confusion_matrix(y_true, y_prediction) 计算,但这只是转移了问题.

The confusion matrix is computed by metrics.confusion_matrix(y_true, y_prediction), but that just shifts the problem.

在@seralouk 的回答之后编辑.在这里,类 -1 被认为是负数,而 01 是正数的变体.

EDIT after @seralouk's answer. Here, the class -1 is to be considered as the negatives, while 0 and 1 are variations of positives.

推荐答案

使用您的数据,您可以一次获得所有类的所有指标:

Using your data, you can get all the metrics for all the classes at once:

import numpy as np
from sklearn.metrics import confusion_matrix

y_true = [1, -1,  0,  0,  1, -1,  1,  0, -1,  0,  1, -1,  1,  0,  0, -1,  0]
y_prediction = [-1, -1,  1,  0,  0,  0,  0, -1,  1, -1,  1,  1,  0,  0,  1,  1, -1]
cnf_matrix = confusion_matrix(y_true, y_prediction)
print(cnf_matrix)
#[[1 1 3]
# [3 2 2]
# [1 3 1]]

FP = cnf_matrix.sum(axis=0) - np.diag(cnf_matrix)  
FN = cnf_matrix.sum(axis=1) - np.diag(cnf_matrix)
TP = np.diag(cnf_matrix)
TN = cnf_matrix.sum() - (FP + FN + TP)

FP = FP.astype(float)
FN = FN.astype(float)
TP = TP.astype(float)
TN = TN.astype(float)

# Sensitivity, hit rate, recall, or true positive rate
TPR = TP/(TP+FN)
# Specificity or true negative rate
TNR = TN/(TN+FP) 
# Precision or positive predictive value
PPV = TP/(TP+FP)
# Negative predictive value
NPV = TN/(TN+FN)
# Fall out or false positive rate
FPR = FP/(FP+TN)
# False negative rate
FNR = FN/(TP+FN)
# False discovery rate
FDR = FP/(TP+FP)
# Overall accuracy
ACC = (TP+TN)/(TP+FP+FN+TN)


对于我们有很多类的一般情况,这些指标在下图中以图形方式表示:


For a general case where we have a lot of classes, these metrics are represented graphically in the following image:

这篇关于python中多类数据的真阳性率和假阳性率(TPR,FPR)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆