事前定義された関数を使用して、スパム フィルターの精度と再現率を探しています
事前定義された関数を使用すると、値 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)