python - 如何计算平均 TPR、TNR、FPR、FNR - 多类分类

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

enter image description here在数据集不平衡的情况下如何计算平均TPR、TNR、FPR、FNR?

示例 FPR:[3.54224720e-04 0.00000000e+00 1.59383505e-05 0.00000000e+00] 那么,我可以计算 4 个类的和除以 4 吗?

TPR:[3.54224720e-04 + 0.00000000e+00 + 1.59383505e-05 + 0.00000000e+00]/4 = 0.99966?

如何计算 3.54224720e-04 它等于 .000354224720 ?

谢谢

FP = np.sum(matrix, axis=0) - np.diag(matrix)
FN = np.sum(matrix, axis=1) - np.diag(matrix)
TP = np.diag(matrix)
TN = np.sum(matrix) - (FP + FN + TP)

# True Positive rate
TPR = TP/(TP+FN)
print("TPR:", TPR)
# True Negative Rate
TNR = TN/(TN+FP)
print("TNR:", TNR)
# False Positive Rate
FPR = FP/(FP+TN)
print("FPR:", FPR)
# False Negative Rate
FNR = FN/(TP+FN)
print("FNR:", FNR)

# Overall accuracy
ACC = (TP+TN)/(TP+FP+FN+TN)
print("ACC :", ACC)

最佳答案

衡量指标平均值的方法有多种。如果您检查包裹,例如sklearn ,您会看到可以提供多个参数。微观、宏观、加权等等。

如果您想手动计算它们,一种方法(微观)是从四个不同的输出中获取不同的 TP、FN、FP 和 TN 值,并将它们加在一起,然后计算您的指标。

因此,您应该真正了解您的问题并看看哪一个是有意义的。大多数情况下,在数据不平衡的情况下,最好使用加权平均。请记住,如果您有任何基线计算,则必须使用完全相同的方法来计算这些值才能进行公平的比较,因为不同的平均方式之间可能存在巨大差异。

是的,这两个数字是相等的。

更新:

如文档所示:

Weighted average: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters ‘macro’ to account for label imbalance; it can result in an F-score that is not between precision and recall.

this question也有关系。

对于加权指标,您可以分别计算 4 个类别中每个类别的每个指标。有了每个类中的实例数,您就可以计算加权平均指标。下图显示了加权精度的方程:

enter image description here

关于python - 如何计算平均 TPR、TNR、FPR、FNR - 多类分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59985557/

相关文章:

python Selenium : WinError 10053 - host is shutting connection down

Python 线程仅启动一个额外的线程

R - 使用什么命令来生成混淆矩阵作为 rpart() 和 Predict() 的输入结果?

python - 如何使用 Tensorflow 创建预测标签和真实标签的混淆矩阵?

python - 构建缺失数据的混淆矩阵

python - Reportlab 文本背景大小与字体大小不匹配

python - python中的循环导入

r - R 中支持向量机和朴素贝叶斯分类器的可变重要性

machine-learning - 前向传播与前向传播前向传球

matrix - 用于类可分离性的散布矩阵