[英]How to compute false positive rate (FPR) and False negative rate percantage?
我计算假阳性率和假阴性率。 我正在使用这些技术:
cnf_matrix=confusion_matrix(Ytest, y_pred)
print(cnf_matrix)
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)
print('FP: '+str(FP))
FN = FN.astype(float)
print('Fn: '+str(FN))
TP = TP.astype(float)
print('FN: '+str(FN))
TN = TN.astype(float)
print('TN: '+str(TN))
# false positive rate
FPR = FP/(FP+TN)
print('FPR: '+str(FPR))
# False negative rate
FNR = FN/(TP+FN)
print('FNR: '+str(FNR))
我得到了这些向量:
FPR: [0. 0.01666667 0. ]
FNR: [0. 0. 0.03448276]
但是,我只需要获得一个值,而不是向量。 如何得到它?
对于多类分类,您的代码似乎是正确的。 向量简单地给出了所有三个类的 FPR 和 FNR。 因为每个班级都会有不同的 FPR 和 FNR。 如果您只对一类的 FPR/FNR 感兴趣,那么您可以通过提供索引来简单地访问该分数
print('FNR: '+str(FNR[0])) #FNR for 1st class will be at index 0
另一方面,对于二元分类,我认为最好使用 scikit-learn 的函数来计算这些值。
FPR = 1 - TNR和TNR = 特异性
FNR = 1 - TPR和TPR = 召回
然后,您可以计算 FPR 和 FNR,如下所示:
from sklearn.metrics import recall_score
tpr = recall_score(Ytest, y_pred) # it is better to name it y_test
# to calculate, tnr we need to set the positive label to the other class
# I assume your negative class consists of 0, if it is -1, change 0 below to that value
tnr = recall_score(Ytest, y_pred, pos_label = 0)
fpr = 1 - tnr
fnr = 1 - tpr
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.