停止在每次字典迭代中增加值 - python

我正在使用Python 2.7。我有两个tsv数据文件,我将它们读入两个字典中,我想计算它们的recall分数,因此我需要计算tpfn
这些是我的字典的样子:

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

谢谢,

参考方案

在我看来,仅当结果中未找到匹配项时,您才要递增fn。您可以使用变量来跟踪是否找到了匹配项,并以此为基础递增fn。在下面,我修改了您的代码,并为此使用了match_found

#iterate gold dictionary
 for i,j in gold.items():

     # create a variable that indicates whether a match was found
     match_found = False

     #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

               # now a match has been found, change variable
               match_found = True

               #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 
     # now, only if no match has been found, increment fn
     if not match_found :
         fn += 1 #<------ wrong calculations caused in this line

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python ThreadPoolExecutor抑制异常 - python

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…

如何用'-'解析字符串到节点js本地脚本? - python

我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…