Harmonic mean in a python function?

后端 未结 2 1687
太阳男子
太阳男子 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 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.

提交回复
热议问题