当前位置: 首页 > news >正文

AP AR

混淆矩阵

真实值=正例真实值=负例
预测值=正例TPFP
预测值=负例FNTN

(根据阈值预测)

P精确度计算:TP/(TP+FP)

R召回率计算:TP/(TP+FN)

AP

综合考虑P R

根据不同的阈值计算出不同的PR组合, 画出PR曲线,计算曲线下面积即为PR

(所有点插值法计算,简单来讲就是近似计算小矩形面积和)

import numpy as np
import matplotlib.pyplot as plt

def calculate_precision_recall(confusion_matrices):
    
    #计算P R
    recall = []
    precision = []

    for tp, fp, fn in confusion_matrices:
        if tp + fp == 0:
            p = 0.0
        else:
            p = tp / (tp + fp)
        if tp + fn == 0:
            r = 0.0
        else:
            r = tp / (tp + fn)
        precision.append(p)
        recall.append(r)

    return recall, precision

def calculate_ap_all_points(recall, precision):
    #所有点插值法计算面积
    recall = np.concatenate(([0.], recall, [1.]))
    precision = np.concatenate(([0.], precision, [0.]))
    for i in range(precision.size - 1, 0, -1):
        precision[i - 1] = np.maximum(precision[i - 1], precision[i])
    ap = np.sum(np.diff(recall) * precision[1:])
    return ap

# 示例 (每个元素为 [TP, FP, FN])
confusion_matrices = [
    [10, 0, 0],  
    [8, 1, 2],  
    [6, 2, 4], 
    [5, 3, 5],  
    [4, 4, 6],  
    [3, 7, 7],  
]

# 计算精确率和召回率
recall, precision = calculate_precision_recall(confusion_matrices)

# 计算AP
ap = calculate_ap_all_points(recall, precision)
print(f"平均精度 (AP): {ap}")

plt.plot(recall, precision, marker='o')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('Precision-Recall Curve (AP = {:.3f})'.format(ap))
plt.grid(True)
plt.show()

AR

平均召回率

主要是考虑漏检,专注于R

import numpy as np

def calculate_ar(true_positives, false_negatives, max_detections):

    recall_values = []
    for tp, fn in zip(true_positives, false_negatives):
        if tp + fn == 0:
            recall = 0.0
        else:
            recall = tp / (tp + fn)
        recall_values.append(recall)

    # 假设我们只考虑前 max_detections 个召回率值
    if len(recall_values) > max_detections:
        recall_values = recall_values[:max_detections]

    if not recall_values:
        return 0.0

    ar = np.mean(recall_values)
    return ar

# 示例数据
true_positives = [10, 8, 6, 5, 4, 3]  # TP
false_negatives = [0, 2, 4, 5, 6, 7]  # FP
max_detections = 5  # 最大检测次数

# 计算AR
ar = calculate_ar(true_positives, false_negatives, max_detections)
print(f"平均召回率 (AR): {ar}")

#计算maxDets 为10时候的AR
max_detections_2 = 10
ar_2 = calculate_ar(true_positives, false_negatives, max_detections_2)
print(f"平均召回率 (AR)maxDets为10 : {ar_2}")

平均精度(Average Precision,AP)以及AP50、AP75、APs、APm、APl、Box AP、Mask AP等不同阈值和细分类别的评估指标说明-CSDN博客

相关文章:

  • 二叉树_4_面试题汇总
  • AlphaGo 家族:从「偷看棋谱」到「自创宇宙套路」的 1008 天
  • 神经网络的基本知识
  • 生态安全的范式
  • LoRa数传、点对点通信、Mesh网络、ZigBee以及图传技术的区别和特点
  • zend server试用分析
  • 架构思维:软件建模与架构设计的关键要点
  • request模块基本使用方法
  • 深入解析java Socket通信中的粘包与拆包问题及解决方案(中)
  • 《灵珠觉醒:从零到算法金仙的C++修炼》卷三·天劫试炼(55)聚宝盆装区间 - 合并区间(排序贪心)
  • 工业数采适配99%协议EG8200Mini 边缘计算网关
  • 扩散模型:AIGC领域的核心引擎,解锁图像生成新维度
  • ruoyi-vue部署
  • “消失的中断“
  • 空地协同智慧探测系统:开启多元探测新时代
  • 优化点列图(RMS半径)的操作数
  • 加密算法逆向与HOOK技术实战
  • 吴恩达机器学习笔记复盘(四)线性回归模型概述
  • Unity编辑器界面扩展——4、Inspector栏UI扩展
  • SpringBoot实现一个Redis限流注解
  • 上海明天短暂恢复晴热最高32℃,一大波雨水在候场
  • 墨西哥海军帆船纽约撞桥事故已致2人死亡19人受伤
  • 推开“房间”的门:一部“生命存在的舞台” 史
  • 墨西哥海军一载两百余人帆船撞上纽约布鲁克林大桥,多人落水
  • 菲律宾选举委员会公布中期选举结果,马科斯阵营选情未达预期
  • 尹锡悦宣布退出国民力量党