gpt4 book ai didi

python-3.x - 计算混淆矩阵中的精确度和召回率

转载 作者:行者123 更新时间:2023-11-30 08:28:09 26 4
gpt4 key购买 nike

假设我有一个如下所示的混淆矩阵。如何计算准确率和召回率?

enter image description here

enter image description here

最佳答案

首先,你的矩阵是颠倒排列的。您想要排列标签,以便在对角线 [(0,0),(1,1),(2,2)] 上设置真阳性,这是您将通过生成的混淆矩阵找到的排列sklearn 和其他软件包。

一旦我们按照正确的方向对事情进行了排序,我们就可以从 this answer 获取页面。并说:

  1. 真阳性位于对角线位置
  2. 误报是按列求和。没有对角线
  3. 假阴性是按行求和。没有对角线。

\然后我们从 sklearn docs 中获取一些公式为了精确度和召回率。并将其全部放入代码中:

import numpy as np
cm = np.array([[2,1,0], [3,4,5], [6,7,8]])
true_pos = np.diag(cm)
false_pos = np.sum(cm, axis=0) - true_pos
false_neg = np.sum(cm, axis=1) - true_pos

precision = np.sum(true_pos / (true_pos + false_pos))
recall = np.sum(true_pos / (true_pos + false_neg))

由于我们删除了真正的阳性来定义假阳性/阴性,只是为了将它们添加回来......我们可以通过跳过几个步骤来进一步简化:

 true_pos = np.diag(cm) 
precision = np.sum(true_pos / np.sum(cm, axis=0))
recall = np.sum(true_pos / np.sum(cm, axis=1))

关于python-3.x - 计算混淆矩阵中的精确度和召回率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40729875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com