2

事前定義された関数を使用して、スパム フィルターの精度と再現率を探しています

事前定義された関数を使用すると、値 1.0 以外を返すことができません。

0.529411764706 の精度結果が得られるはずなので、これが正しくないことはわかっています。

また、何らかの理由で各リストの最初のエントリが数値ではないため、pop を使用しているため、append(int(...

関数は次のとおりです。

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

これが私のコードです:

import hw10_lib
from hw10_lib import precision
from hw10_lib import recall

actual = []
for line in open("/path/hw10.ref", 'r'):
    actual.append(line.strip().split('\t')[-1])
actual.pop(0)

predicted = []
for line in open("/path/hw10.hyp", 'r'):
    predicted.append(line.strip().split('\t')[-1])
predicted.pop(0)

prec = precision(actual, predicted)
rec = recall(actual, predicted)

print ('Precision: ', prec)
print ('Recall: ', rec)
4

1 に答える 1

1

関数で文字列を数値として扱っています。文字列が空でない場合、bool(aString) のテストは常に true になります。

有効なフィールドを関数に渡す前に浮動小数点に変換するか、値をループするときに関数内で変換します。

bool("0") # True
bool("1") # True

すべてが常に True の場合、1 / 1 == 1 および 100 / 100 == 1

また、float の精度を維持するために、int ではなく float を分割することを忘れないでください。

    for i in range(n):
        if float(hyp[i]):
            np += 1.0
            if float(ref[i]):
                ntp += 1.0
    return ntp/np

元のリストに値を適切に追加することもできます。

for line in open("/path/hw10.ref", 'r'):
    try:
        val = float(line.strip().split('\t')[-1])
    except:
        continue
    actual.append(val)

次に、有効なフロートのみがあり、ポップする必要はありません。

于 2012-12-13T06:58:11.017 に答える