【问题标题】:How to compute false positive rate (FPR) and False negative rate percantage?如何计算误报率(FPR)和误报率百分比?
【发布时间】:2020-10-12 06:49:14
【问题描述】:

我计算误报率和误报率。我正在使用这些技术:

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]

但是,我只需要获取一个值,而不是向量。 如何得到它?

【问题讨论】:

  • confusion_matrix sklearn.metrics.confusion_matrix?
  • 是的,它是 sklearn.metrics.confusion_matrix
  • 你有几节课?如果您期望一个值,则表明您只有两个类,所以我不确定您为什么会得到混淆矩阵的切片。
  • 我有三门课
  • 那么,你认为“积极”还是“消极”是什么?

标签: python scikit-learn


【解决方案1】:

对于多类分类,您的代码似乎是正确的。这些向量只是给出了所有三个类别的 FPR 和 FNR。因为每个班级会有不同的 FPR 和 FNR。如果您只对某一类的 FPR/FNR 感兴趣,那么您可以通过给出索引来访问该分数

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

另一方面,对于二元分类,我认为最好使用 scikit-learn 的函数来计算这些值。

  • FPR = 1 - TNRTNR = 特异性

  • FNR = 1 - TPRTPR = 召回

然后,您可以计算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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多