• 【深度学习】【评价指标】多标签分类器的评价指标


    这么多评价指标:
    https://scikit-learn.org/stable/modules/classes.html#module-sklearn.metrics
    尤其对这个感兴趣:
    https://scikit-learn.org/stable/modules/generated/sklearn.metrics.average_precision_score.html

    别样的一种:

    # Copyright (c) OpenMMLab. All rights reserved.
    import numpy as np
    import torch
    
    
    def average_precision(pred, target):
        r"""Calculate the average precision for a single class.
    
        AP summarizes a precision-recall curve as the weighted mean of maximum
        precisions obtained for any r'>r, where r is the recall:
    
        .. math::
            \text{AP} = \sum_n (R_n - R_{n-1}) P_n
    
        Note that no approximation is involved since the curve is piecewise
        constant.
    
        Args:
            pred (np.ndarray): The model prediction with shape (N, ).
            target (np.ndarray): The target of each prediction with shape (N, ).
    
        Returns:
            float: a single float as average precision value.
        """
        eps = np.finfo(np.float32).eps
    
        # sort examples
        sort_inds = np.argsort(-pred)
        sort_target = target[sort_inds]
    
        # count true positive examples
        pos_inds = sort_target == 1
        tp = np.cumsum(pos_inds)
        total_pos = tp[-1]
    
        # count not difficult examples
        pn_inds = sort_target != -1
        pn = np.cumsum(pn_inds)
    
        tp[np.logical_not(pos_inds)] = 0
        precision = tp / np.maximum(pn, eps)
        ap = np.sum(precision) / np.maximum(total_pos, eps)
        return ap
    
    
    def mAP(pred, target):
        """Calculate the mean average precision with respect of classes.
    
        Args:
            pred (torch.Tensor | np.ndarray): The model prediction with shape
                (N, C), where C is the number of classes.
            target (torch.Tensor | np.ndarray): The target of each prediction with
                shape (N, C), where C is the number of classes. 1 stands for
                positive examples, 0 stands for negative examples and -1 stands for
                difficult examples.
    
        Returns:
            float: A single float as mAP value.
        """
        if isinstance(pred, torch.Tensor) and isinstance(target, torch.Tensor):
            pred = pred.detach().cpu().numpy()
            target = target.detach().cpu().numpy()
        elif not (isinstance(pred, np.ndarray) and isinstance(target, np.ndarray)):
            raise TypeError('pred and target should both be torch.Tensor or'
                            'np.ndarray')
    
        assert pred.shape == \
               target.shape, 'pred and target should be in the same shape.'
        num_classes = pred.shape[1]
        ap = np.zeros(num_classes)
        for k in range(num_classes):
            ap[k] = average_precision(pred[:, k], target[:, k])
        mean_ap = ap.mean() * 100.0
        return mean_ap
    
    
    if __name__ == '__main__':
        pred = np.asarray([[0.1, 0.2, 0.3, 0.6, 0.7, 0.4, 0.12, 0.11, 0.8, 0.9],
                           [0.1, 0.2, 0.3, 0.6, 0.7, 0.4, 0.12, 0.11, 0.8, 0.9]])
        target = np.asarray([[1, 1, 0, 0, 0, 1, 1, 1, 1, 1],
                             [0, 1, 0, 1, 1, 1, 1, 1, 1, 1]])
        print(mAP(pred, target))
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    改日再聊~

  • 相关阅读:
    java常用类
    juc
    【JavaWeb】
    【面试经典150 | 滑动窗口】长度最小的子数组
    计算机竞赛 机器视觉目标检测 - opencv 深度学习
    esp32 idf 添加的compones找不到头文件
    对音频切分成小音频(机器学习用)
    1.Node.js-函数和匿名函数的用法
    GPUImage框架使用
    pdf怎么合并成一个pdf?超实用的几种合并方法
  • 原文地址:https://blog.csdn.net/x1131230123/article/details/126179679