2

適合率と再現率のスコアを与える2つの関数があります。これらの2つのスコアを使用する同じライブラリで、調和平均関数を定義する必要があります。関数は次のようになります。

関数は次のとおりです。

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

調和平均関数はどのようになりますか?私が持っているのはこれだけですが、私はそれが正しくないことを知っています:

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

2 に答える 2

3

以下は、任意の数の引数で機能します。

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

precisionとの調和平均を計算するには、次をrecall使用します。

result = hmean(precision, recall)

関数には2つの問題があります。

  1. 値を返すことができません。
  2. Pythonの一部のバージョンでは、整数引数に整数除算を使用して、結果を切り捨てます。
于 2012-12-13T08:10:50.573 に答える
0

関数を少し変更し、定義したものと同じ関数を使用すると、次のように機能しF1ます。precisionrecall

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ます。

于 2012-12-13T09:02:56.670 に答える