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

标签 python machine-learning scikit-learn confusion-matrix multiclass-classification

<分区>

您如何计算多类分类问题的正确率和错误率?说,

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) 计算,但这只是转移了问题。


在@seralouk 回答后进行编辑。此处,类 -1 被视为负类,而 01 是正类的变体。

最佳答案

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

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)

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

Confusion matrix multiclass

关于python - python中多类数据的真阳性率和假阳性率(TPR,FPR),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50666091/

相关文章:

python - 如何根据 python 中最近的集群质心逻辑将新观察分配给现有的 Kmeans 集群?

python - 如何解决FunctionError和MapError

python - 在特定索引上启动 iterrows() 循环

python - appengine ndb 按种类名称初始化模型

python-3.x - Scikit-learn 对于 MLR 的正确性?

r - 使用 R 中的 apply 函数族进行向量化

python - 使用整数索引重新采样数据帧

python - 检查目标 : expected activation_2 to have shape (512, 时出错,但得到形状为 (1,) 的数组

python - 默认的 sklearn TfidfVectorizer 预处理器是做什么的?

python - 如何使用 scikit 正确进行一种热编码?