【问题标题】:Stop a value from being incremented in each dictionary iteration阻止值在每次字典迭代中递增
【发布时间】:2019-02-24 18:16:28
【问题描述】:

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

gold = {'A11':'cat', 'A22':'cat', 'B3':'mouse'}
results = {'A2':'cat', 'B2':'dog'}

我的代码主要是迭代gold字典,去掉gold字典keyresultskey末尾的数字。然后,检查键是否匹配以查找它们的值是否匹配以计算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 在每次迭代中递增?任何指导将不胜感激。

谢谢,

【问题讨论】:

  • 把“contiue”改成“continue”会有帮助吗?
  • @bobbel 哈哈,这只是从 ide 复制代码时的拼写错误。但是,fn 仍然在每次迭代中递增
  • ................
  • 您很可能需要将控制 fn 增加的行放在 if 条件之一下
  • @jpobst 谢谢你的建议!现在问题更易读、更整洁了。

标签: python python-2.7 loops dictionary


【解决方案1】:

如果这不是您所需要的,您应该能够对其进行修改以使其正常工作。

tp = 0 #true positives
fn = 0 #false negatives
fp = 0 #false positives


gold = {'A11':'cat', 'A22':'cat', 'B3':'mouse'}
results = {'A2':'cat', 'B2':'dog'}

for gold_k, gold_v in gold.items():
    # Remove digits and make lower case
    clean_gold_k = gold_k.rstrip(string.digits).lower()

    for results_k, results_v in results.items():
        # Remove digits and make lower case
        clean_results_k = results_k.rstrip(string.digits).lower()

        keys_agree = clean_gold_k == clean_results_k
        values_agree = gold_v.lower() == results_v.lower()

        print('\n-------------------------------------')
        print('Gold = ' + gold_k + ': ' + gold_v)
        print('Result = ' + results_k + ': ' + results_v)

        if keys_agree and values_agree:
            print('tp')
            tp += 1

        elif keys_agree and not values_agree:
            print('fn')
            fn += 1

        elif values_agree and not keys_agree:
            print('fp')
            fp += 1

【讨论】:

  • 谢谢。但它算 2 tp!只有一个tp。所以也许你应该删除问题中显示的计数记录。 fn 也应该是 2 而不是 1。谢谢
  • 我希望 fn 仅在遍历整个结果字典并找到任何 tp 后才递增。谢谢
【解决方案2】:

在我看来,只有在结果中找不到匹配项时,您才想增加 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

【讨论】:

  • YESSS 我正要回答我自己的问题,我添加了相同的变量,现在它完美地工作了!!谢谢哇,这太简单了,我在想什么;d 再次感谢
猜你喜欢
  • 2022-12-30
  • 2021-03-08
  • 2019-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多