How do you count correct predictions from a square confusion matrix?

trace sums a matrix diagonal. In a confusion matrix with identically ordered labels on both axes, the main diagonal counts correct predictions.

numpy.trace in practice

The diagonal of [[8,2],[1,9]] totals 17. Divide by all 20 observations for accuracy. Realign axes first if their label orders differ.

import numpy as np
np.trace([[8,2],[1,9]])

Result 17

中文

怎样从方形混淆矩阵快速得到判对总数?

trace 沿矩阵对角线求和。行列使用相同类别次序时,主对角线是预测与真值相符的计数。

numpy.trace 算例

矩阵 [[8,2],[1,9]] 对角线和为 17。若要报准确率,再除以总样本数 20。若标签错位先重新对齐。

import numpy as np
np.trace([[8,2],[1,9]])

结果 17

References / 参考来源

Keep reading / 继续阅读