zoukankan      html  css  js  c++  java
  • AUC计算

    首先AUC值是一个概率值,当你随机挑选一个正样本以及负样本,当前的分类算法根据计算得到的Score值将这个正样本排在负样本前面的概率就是AUC值,AUC值越大,当前分类算法越有可能将正样本排在负样本前面,从而能够更好地分类。

    AUC计算

    • 最直观的,根据AUC这个名称,我们知道,计算出ROC曲线下面的面积,就是AUC的值。事实上,这也是在早期 Machine Learning文献中常见的AUC计算方法。由于我们的测试样本是有限的。我们得到的AUC曲线必然是一个阶梯状的。因此,计算的AUC也就是这些阶梯 下面的面积之和。这样,我们先把score排序(假设score越大,此样本属于正类的概率越大),然后一边扫描就可以得到我们想要的AUC。但是,这么 做有个缺点,就是当多个测试样本的score相等的时候,我们调整一下阈值,得到的不是曲线一个阶梯往上或者往右的延展,而是斜着向上形成一个梯形。此 时,我们就需要计算这个梯形的面积。由此,我们可以看到,用这种方法计算AUC实际上是比较麻烦的。

    • 一个关于AUC的很有趣的性质是,它和Wilcoxon-Mann-Witney Test是等价的。而Wilcoxon-Mann-Witney Test就是测试任意给一个正类样本和一个负类样本,正类样本的score有多大的概率大于负类样本的score。有了这个定义,我们就得到了另一种计 算AUC的办法。我们知道,在有限样本中我们常用的得到概率的办法就是通过频率来估计之。这种估计随着样本规模的扩大而逐渐逼近真实值。这 和上面的方法中,样本数越多,计算的AUC越准确类似;也和积分时,小区间越细计算越准确同样道理。具体来说就是统计一下所有的 M×N(M为正类样本的数目,N为负类样本的数目)个正负样本对中,有多少个组中的正样本的score大于负样本的score。当二元组中正负样本的 score相等的时候,按照0.5计算。然后除以MN。实现这个方法的复杂度为O(n^2)。n为样本数(即n=M+N)

    • 第三种方法实际上和上述第二种方法是一样的,但是复杂度减小了。它也是首先对score从大到小排序,然后令最大score对应的sample 的rank为n,第二大score对应sample的rank为n-1,以此类推。然后把所有的正类样本的rank相加,再减去M-1种两个正样本组合的情况。得到的就是所有的样本中有多少对正类样本的score大于负类样本的score。然后再除以M×N。即

    AUC = frac{ sum_{iin positive} rank - frac{M(1+M)}2}{M	imes N}
    

    附代码实现:

    # auc with a formular I don't know how to work..
    # Params is a list with tuple contains (precision, label)
    # Return a float auc value
    #
    def auc_only_distinct(_pr_label_tuple_seq = []):
      _pr_label_tuple_seq.sort(key=lambda pair : pair[0])
      positive_sample_count = sum([1 for pair in _pr_label_tuple_seq if pair[1] == 1 ])
      negtive_sample_count = len(_pr_label_tuple_seq) - positive_sample_count
      sigma = 1.0 * 
        sum(
          [ pair[0] for pair in
            zip(range(1, 1 + len(_pr_label_tuple_seq)), _pr_label_tuple_seq)
            if pair[1][1] == 1])
      return (sigma - (positive_sample_count + 1) * positive_sample_count / 2) / positive_sample_count / negtive_sample_count
    
    # auc with Wilcoxon-Mann-Witney Test and dynamic programming
    # same as aucByAngel_only_distinct when precision is distinct and same precision all is postive sample
    # is different from aucByAngel_only_distinct when same precision but not all positive sample
    # Params is a list with tuple contains (precision, label)
    # Return a float auc value
    #
    def auc(_pr_label_tuple_seq = []):
      _pr_label_tuple_seq.sort(key=lambda pair : pair[0])
      _pr_seq = [ i[0] for i in _pr_label_tuple_seq ]
      _pr_rank_tuple_dict = {}
      _pr_rank_tuple_seq = zip(_pr_seq, range(1, len(_pr_label_tuple_seq)+1))
      for _pr_rank_tuple in _pr_rank_tuple_seq:
        precision = _pr_rank_tuple[0]
        rank = _pr_rank_tuple[1]
        if(_pr_rank_tuple_dict.has_key(precision)):
             _rank_count_tuple = _pr_rank_tuple_dict[precision]
             old_count = _rank_count_tuple[1]
             new_count = old_count + 1
             rank = (_rank_count_tuple[0] * old_count + rank) / new_count
             _pr_rank_tuple_dict[precision] = (rank, new_count)
        else:
             _pr_rank_tuple_dict[precision] = (rank * 1.0, 1)
      positive_sample_count = sum([1 for pair in _pr_label_tuple_seq if pair[1] == 1 ])
      negtive_sample_count = len(_pr_label_tuple_seq) - positive_sample_count
      sigma = 1.0 * sum([ _pr_rank_tuple_dict.get(pair[0])[0] for pair in _pr_label_tuple_seq if pair[1] == 1])
      return (sigma - (positive_sample_count + 1) * positive_sample_count / 2) / positive_sample_count / negtive_sample_count
    
  • 相关阅读:
    Win8系统 Python安装
    一些安卓开源框架整理
    Android 媒体键监听以及模拟媒体键盘的实现 demo
    android View 自动 GONE 问题
    Android 定时器TimerTask 简单使用
    关于Android studio 相对 eclipse 优点
    Java序列化与反序列化
    android shape的使用 边框
    Android Studio 修改 包名 package name
    Android WebView Long Press长按保存图片到手机
  • 原文地址:https://www.cnblogs.com/suanec/p/9996644.html
Copyright © 2011-2022 走看看