python中多类数据的真阳性率和假阳性率(TPR,FPR)

编程入门 行业动态 更新时间:2024-10-09 14:17:51
本文介绍了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 被认为是负数,而 0 和 1 是正数的变体.

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)

本文发布于:2023-11-29 10:52:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646184.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阳性率   数据   中多类   python   TPR

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!