Harmonic mean in a python function?

偶尔善良 提交于 2019-12-02 02:43:05

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.

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.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!