Harmonic mean in a python function?

后端 未结 2 1654
太阳男子
太阳男子 2021-01-23 05:16

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

相关标签:
2条回答
  • 2021-01-23 05:53

    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
    

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

    0 讨论(0)
  • 2021-01-23 06:09

    The following will work with any number of arguments:

    def hmean(*args):
        return len(args) / sum(1. / val for val in args)
    

    To compute the harmonic mean of precision and recall, use:

    result = hmean(precision, recall)
    

    There are two problems with your function:

    1. It fails to return a value.
    2. On some versions of Python, it would use integer division for integer arguments, truncating the result.
    0 讨论(0)
提交回复
热议问题