阻止值在每次字典迭代中递增

我正在使用 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在每次迭代中递增?任何指导将不胜感激。


谢谢,


ITMISS
浏览 126回答 2
2回答

森栏

在我看来,您只想fn在结果中找不到匹配项时才增加。您可以使用变量来跟踪是否已找到匹配项,并在此基础上增加fn。在下面,我调整了您的代码并用于match_found此目的。#iterate gold dictionary&nbsp;for i,j in gold.items():&nbsp; &nbsp; &nbsp;# create a variable that indicates whether a match was found&nbsp; &nbsp; &nbsp;match_found = False&nbsp; &nbsp; &nbsp;#remove the digits off gold keys&nbsp; &nbsp; &nbsp;i_stripped = i.rstrip(string.digits)&nbsp; &nbsp; &nbsp;#iterate results dictionary&nbsp; &nbsp; &nbsp;for k,v in results.items():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#remove the digits off results keys&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k_stripped = k.rstrip(string.digits)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# check if key match!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if i_stripped == k_stripped:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#check if values match then increment tp&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if j == v:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tp += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# now a match has been found, change variable&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;match_found = True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#delete dictionary entries to avoid counting them again&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;del gold_copy[i]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;del results_copy[k]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#get out of this loop we found a match!&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;continue&nbsp; &nbsp; &nbsp;# NO match was found in the results, then consider it as fn&nbsp;&nbsp; &nbsp; &nbsp;# now, only if no match has been found, increment fn&nbsp; &nbsp; &nbsp;if not match_found :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fn += 1 #<------ wrong calculations caused in this line

HUX布斯

如果这不是您所需要的,您应该能够对其进行修改以使其正常工作。tp = 0 #true positivesfn = 0 #false negativesfp = 0 #false positivesgold = {'A11':'cat', 'A22':'cat', 'B3':'mouse'}results = {'A2':'cat', 'B2':'dog'}for gold_k, gold_v in gold.items():&nbsp; &nbsp; # Remove digits and make lower case&nbsp; &nbsp; clean_gold_k = gold_k.rstrip(string.digits).lower()&nbsp; &nbsp; for results_k, results_v in results.items():&nbsp; &nbsp; &nbsp; &nbsp; # Remove digits and make lower case&nbsp; &nbsp; &nbsp; &nbsp; clean_results_k = results_k.rstrip(string.digits).lower()&nbsp; &nbsp; &nbsp; &nbsp; keys_agree = clean_gold_k == clean_results_k&nbsp; &nbsp; &nbsp; &nbsp; values_agree = gold_v.lower() == results_v.lower()&nbsp; &nbsp; &nbsp; &nbsp; print('\n-------------------------------------')&nbsp; &nbsp; &nbsp; &nbsp; print('Gold = ' + gold_k + ': ' + gold_v)&nbsp; &nbsp; &nbsp; &nbsp; print('Result = ' + results_k + ': ' + results_v)&nbsp; &nbsp; &nbsp; &nbsp; if keys_agree and values_agree:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('tp')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tp += 1&nbsp; &nbsp; &nbsp; &nbsp; elif keys_agree and not values_agree:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('fn')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fn += 1&nbsp; &nbsp; &nbsp; &nbsp; elif values_agree and not keys_agree:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('fp')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fp += 1
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python