如何计算多类分类问题中的真正率和假正率?例如,
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)
计算的,但这只是将问题转移了。
在@的人回答后编辑。这里,类-1
被视为负类,而0
和1
被视为正类的不同变体。
回答:
使用你的数据,你可以一次性获取所有类别的所有指标:
import numpy as npfrom sklearn.metrics import confusion_matrixy_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)# 敏感度、命中率、召回率或真正率TPR = TP/(TP+FN)# 特异度或真负率TNR = TN/(TN+FP) # 精确度或阳性预测值PPV = TP/(TP+FP)# 阴性预测值NPV = TN/(TN+FN)# 误报率或假正率FPR = FP/(FP+TN)# 假负率FNR = FN/(TP+FN)# 假发现率FDR = FP/(TP+FP)# 总体准确率ACC = (TP+TN)/(TP+FP+FN+TN)
对于有很多类的一般情况,这些指标在下图中以图形方式表示: