准确率,即成功识别目标对象、甄别非目标对象 占总数的比例
在处理多分类问题时,我们常常需要查看具体类别的分类准确率,这里提供一种计算各类别准确率的方法
targ = [1,2,3,4,5,6,7,8,9, 1, 2,3,4,5,6,7,8,9]
pred = [1,2,3,4,5,6,7,8,9, 2, 1,3,4,5,6,7,8,9] # 交换一对1 2的顺序
# 将各个训练批次的标签汇总到一起
# pred += predicted_labels.tolist() # 从神经网络中读取的数据为tensor,需要转变为list
# targ += targets.tolist() # 不然会出现错误:
# RuntimeError: Boolean value of Tensor with more than one value is ambiguous
for i in range(9): # 种类的数量
denominator = targ.count(i) # 从list里获取i的数量,得到各类的样本数量
numerator = np.equal(targ, i) & np.equal(pred, i)
# 分别从真值、预测值中筛选目标值,并与计算 得出正确的个体
numerator = np.sum(numerator==1) # 统计所有正确的个体
print('Accuracy of {:2d}: {:.5f}'.format(i,numerator/denominator))
print('Total accuracy : {:.5f} %%'.format(accuracy_score(targ, pred)))
# 输出结果
# Accuracy of 0: nan
# Accuracy of 1: 0.50000
# Accuracy of 2: 0.50000
# Accuracy of 3: 1.00000
# Accuracy of 4: 1.00000
# Accuracy of 5: 1.00000
# Accuracy of 6: 1.00000
# Accuracy of 7: 1.00000
# Accuracy of 8: 1.00000
# Total accuracy : 0.88889 %%