Python 实现 FPR, TPR, Recall, Precision 计算函数

本文将提供 Python 代码实现假阳性率 (False Positive Rate, FPR), 真阳性率 (True Positive Rate, TPR), 召回率 (Recall) 和 精确度 (Precision) 的计算函数。这些指标在评估二分类模型性能时至关重要。

1. 计算 FPR (假阳性率)

def calculate_fpr(true_negatives, false_positives):
    total_negatives = true_negatives + false_positives
    fpr = false_positives / total_negatives
    return fpr

2. 计算 TPR (真阳性率)

def calculate_tpr(true_positives, false_negatives):
    total_positives = true_positives + false_negatives
    tpr = true_positives / total_positives
    return tpr

3. 计算 Recall (召回率)

def calculate_recall(true_positives, false_negatives):
    total_positives = true_positives + false_negatives
    recall = true_positives / total_positives
    return recall

4. 计算 Precision (精确度)

def calculate_precision(true_positives, false_positives):
    total_predicted_positives = true_positives + false_positives
    precision = true_positives / total_predicted_positives
    return precision

以上函数分别计算了假阳性率 (FPR), 真阳性率 (TPR), 召回率 (Recall) 和 精确度 (Precision)。这些指标在评估二分类模型的性能时非常常用。


原文地址: http://www.cveoy.top/t/topic/nj46 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录