首先,你的矩阵是倒置的。您想排列标签,以便在对角线上设置真正的正数 [(0,0),(1,1),(2,2)] sklearn 和其他软件包。
一旦我们按照正确的方向对事情进行了排序,我们就可以从这个答案中取出一页并说:
\ 然后我们从 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))
Run Code Online (Sandbox Code Playgroud)
由于我们删除了真阳性来定义 false_positives/negatives 只是为了将它们添加回来......我们可以通过跳过几个步骤来进一步简化:
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))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6021 次 |
| 最近记录: |