precision_recall.py 文件源码

python
阅读 50 收藏 0 点赞 0 评论 0

项目:antgo 作者: jianzfb 项目源码 文件源码
def dcg_k(r,k,method = 0):
    '''
    Score is discounted cumulative gain (dcg)
    Relevance is positive real values.  Can use binary
    as the previous methods.
    Example from
    http://www.stanford.edu/class/cs276/handouts/EvaluationNew-handout-6-per.pdf
    Parameters
    ----------
    r: Relevance scores (list or numpy) in rank order
            (first element is the first item)
    k: Number of results to consider
    method: 0 or 1

    Returns
    -------
    Discounted cumulative gain
    '''
    r = np.asfarray(r)[:k]
    if r.size:
        if method == 0:
            #standard definition
            return r[0] + np.sum(r[1:] / np.log2(np.arange(2, r.size + 1)))
        elif method == 1:
            #used in Kaggle
            return np.sum((np.power(2,r) - 1.0) / np.log2(np.arange(2, r.size + 2)))
        else:
            raise ValueError('method must be 0 or 1.')
    return 0.
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号