一次计算精度,召回率和F得分-python [英] Calculating Precision, Recall and F-score in one pass - python

查看:511
本文介绍了一次计算精度,召回率和F得分-python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

准确性,精确度,召回率和f分数是衡量机器学习系统中系统质量的指标.这取决于正确/错误肯定/否定的混淆矩阵.

Accuracy, precision, recall and f-score are measures of a system quality in machine-learning systems. It depends on a confusion matrix of True/False Positives/Negatives.

考虑到二进制分类任务,我尝试了以下方法来获得返回精度,精度,召回率和f分数的函数:

Given a binary classification task, I have tried the following to get a function that returns accuracy, precision, recall and f-score:

gold = [1] + [0] * 9
predicted = [1] * 10

def evaluation(gold, predicted):
  true_pos = sum(1 for p,g in zip(predicted, gold) if p==1 and g==1)
  true_neg = sum(1 for p,g in zip(predicted, gold) if p==0 and g==0)
  false_pos = sum(1 for p,g in zip(predicted, gold) if p==1 and g==0)
  false_neg = sum(1 for p,g in zip(predicted, gold) if p==0 and g==1)
  try:
    recall = true_pos / float(true_pos + false_neg)
  except:
    recall = 0
  try:
    precision = true_pos / float(true_pos + false_pos)
  except:
    precision = 0
  try:
    fscore = 2*precision*recall / (precision + recall)
  except:
    fscore = 0
  try:
    accuracy = (true_pos + true_neg) / float(len(gold))
  except:
    accuracy = 0
  return accuracy, precision, recall, fscore

但是似乎我已经遍历了4次数据集以获得真/假肯定/否定".

But it seems like I have redundantly looped through the dataset 4 times to get the True/False Positives/Negatives.

也可以用多个try-excepts来捕获ZeroDivisionError.

那么在数据集中没有多次循环的情况下,获取真假阳性/假阳性计数的pythonic方法是什么?

如何在没有多个try-except的情况下以Python的方式捕获ZeroDivisionError?

How do I pythonically catch the ZeroDivisionError without the multiple try-excepts?

我还可以执行以下操作在一个循环中计算正确/错误肯定/否定,但是是否存在另一种不使用多个if的替代方法?:

I could also do the following to count the True/False Positives/Negatives in one loop but is there an alternative way without the multiple if?:

for p,g in zip(predicted, gold):
    if p==1 and g==1:
        true_pos+=1
    if p==0 and g==0:
        true_neg+=1
    if p==1 and g==0:
        false_pos+=1
    if p==0 and g==1:
        false_neg+=1

推荐答案

获取True/False计数的pythonic方法是什么 在数据集中没有多个循环的阳性/阴性?

what is the pythonic way to get the counts of the True/False Positives/Negatives without multiple loops through the dataset?

我会使用 collections.Counter ,大致就是您正在做的事情最后,所有if(您应该使用elif s,因为您的条件是互斥的)

I would use a collections.Counter, roughly what you're doing with all of the ifs (you should be using elifs, as your conditions are mutually exclusive) at the end:

counts = Counter(zip(predicted, gold))

然后例如true_pos = counts[1, 1].

如何在不使用倍数的情况下以Python方式捕获ZeroDivisionError 尝试例外吗?

How do I pythonically catch the ZeroDivisionError without the multiple try-excepts?

首先,您(几乎)应该永远不要使用裸露的except:.如果要捕获ZeroDivisionError,请写except ZeroDivisionError.您还可以考虑使用 跨越式发展" 方法,在尝试除法之前检查分母是否为0,例如

For a start, you should (almost) never use a bare except:. If you're catching ZeroDivisionErrors, then write except ZeroDivisionError. You could also consider a "look before you leap" approach, checking whether the denominator is 0 before trying the division, e.g.

accuracy = (true_pos + true_neg) / float(len(gold)) if gold else 0

这篇关于一次计算精度,召回率和F得分-python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆