简体   繁体   English

如何计算误报率 (FPR) 和误报率百分比?

[英]How to compute false positive rate (FPR) and False negative rate percantage?

I compute the False positive rate and False negative rate.我计算假阳性率和假阴性率。 I am using these techniques:我正在使用这些技术:

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))

I got these vectors:我得到了这些向量:

FPR: [0.         0.01666667 0.        ]
FNR: [0.         0.         0.03448276]

However, I need to get just one value, not a vector.但是,我只需要获得一个值,而不是向量。 How to get that?如何得到它?

Your code seems correct, for multiclass classification.对于多类分类,您的代码似乎是正确的。 The vectors simply give the FPR and FNR for all three classes.向量简单地给出了所有三个类的 FPR 和 FNR。 Because there will be different FPR and FNR for each class .因为每个班级都会有不同的 FPR 和 FNR。 If you are just interested in FPR/FNR of one class, then you can simply access that score by giving the index如果您只对一类的 FPR/FNR 感兴趣,那么您可以通过提供索引来简单地访问该分数

print('FNR: '+str(FNR[0]))   #FNR for 1st class will be at index 0

On the other hand, for binary classification, I think it is better to use scikit-learn's functions to calculate these values.另一方面,对于二元分类,我认为最好使用 scikit-learn 的函数来计算这些值。

  • FPR = 1 - TNR and TNR = specificity FPR = 1 - TNRTNR = 特异性

  • FNR = 1 - TPR and TPR = recall FNR = 1 - TPRTPR = 召回

Then, you can calculate FPR and FNR as below:然后,您可以计算 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM