Я использую Python 2.7.У меня есть два файла данных tsv, которые я прочитал в два словаря, и я хотел бы вычислить их recall
, поэтому мне нужно вычислить tp
и fn
.Вот как выглядят мои словари:
gold = {'A11':'cat', 'A22':'cat', 'B3':'mouse'}
results = {'A2':'cat', 'B2':'dog'}
Мой код в основном повторяет словарь gold
и удаляет цифры в конце словаря gold
key
, а также results
key
.Затем проверяет, совпадают ли ключи, чтобы определить, совпадают ли их значения, и вычислить tp
.Однако мой код, кажется, всегда увеличивает fn
.Вот мой исполняемый код:
from __future__ import division
import string
def eval():
tp=0 #true positives
fn=0 #false negatives
fp=0#false positives
gold = {'A11':'cat', 'A22':'cat', 'B3':'mouse'}
results = {'A2':'cat', 'B2':'dog'}
#iterate gold dictionary
for i,j in gold.items():
#remove the digits off gold keys
i_stripped = i.rstrip(string.digits)
#iterate results dictionary
for k,v in results.items():
#remove the digits off results keys
k_stripped = k.rstrip(string.digits)
# check if key match!
if i_stripped == k_stripped:
#check if values match then increment tp
if j == v:
tp += 1
#delete dictionary entries to avoid counting them again
del gold_copy[i]
del results_copy[k]
#get out of this loop we found a match!
break
continue
# NO match was found in the results, then consider it as fn
fn += 1 #<------ wrong calculations caused in this line
print 'tp = %.2f fn = %.2f recall = %.2f ' % (tp, fn, float(tp)/(tp+fn))
, и это вывод:
tp = 1.00 fn = 3.00 recall = 0.25
fn
неверен, он должен быть 2
вместо 3
.Как я могу остановить увеличение fn
на каждой итерации?Любое руководство будет по достоинству оценено.
Спасибо,