python函数中的谐波意思是? [英] Harmonic mean in a python function?

查看:213
本文介绍了python函数中的谐波意思是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个函数给出精确度和查全率,我需要在使用这两个分数的同一个库中定义一个谐波均值函数.函数看起来像这样:

I have 2 functions that give out precision and recall scores, I need to make a harmonic mean function defined in the same library that uses these two scores. The functions looks like this:

以下是功能:

def precision(ref, hyp):
    """Calculates precision.
    Args:
    - ref: a list of 0's and 1's extracted from a reference file
    - hyp: a list of 0's and 1's extracted from a hypothesis file
    Returns:
    - A floating point number indicating the precision of the hypothesis
    """
    (n, np, ntp) = (len(ref), 0.0, 0.0)
    for i in range(n):
            if bool(hyp[i]):
                    np += 1
                    if bool(ref[i]):
                            ntp += 1
    return ntp/np

def recall(ref, hyp):
    """Calculates recall.
    Args:
    - ref: a list of 0's and 1's extracted from a reference file
    - hyp: a list of 0's and 1's extracted from a hypothesis file
    Returns:
    - A floating point number indicating the recall rate of the hypothesis
    """
    (n, nt, ntp) = (len(ref), 0.0, 0.0)
    for i in range(n):
            if bool(ref[i]):
                    nt += 1
                    if bool(hyp[i]):
                            ntp += 1
    return ntp/nt

谐波均值函数是什么样子? 我所拥有的只是这个,但我知道这是不对的:

What would the harmonic mean function look like? All I have is this but I know its not right:

def F1(precision, recall):
    (2*precision*recall)/(precision+recall)

推荐答案

对您的F1函数稍作更改,并使用与您定义的相同的precisionrecall函数,我可以正常工作:

With a slight change of your F1 function, and with the same precision and recall function you defined, I have this working:

def F1(precision, recall):
    return (2*precision*recall)/(precision+recall)

r = [0,1,0,0,0,1,1,0,1]
h = [0,1,1,1,0,0,1,0,1]
p = precision(r, h)
rec = recall(r, h)
f = F1(p, rec)
print f

特别回顾一下我拥有的变量的使用.您必须计算每个函数的结果并将其传递给F1函数.

Review especially the use of variables I have. You must compute the result of each function and pass them to the F1 function.

这篇关于python函数中的谐波意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆