机器学习算法--分类的衡量指标(误差矩阵或混淆矩阵)

对分类器而言,我们希望它做对两件事:一是用类标签标记真正属于该类的对象,二是不用这个标签去标记不属于此类的对象。这两件事对应得到的正确计数值分别称为真阳(true positive)和真阴(true negative)。真阳、假阳、真阴、假阴这 4 个数合并成一个 2 × 2 矩阵,称为误差矩阵或混淆矩阵

import pandas as pd
import numpy as np

y_true = np.array([0, 0, 0, 1, 1, 1, 1, 1, 1, 1])
y_pred = np.array([0, 0, 1, 1, 1, 1, 1, 0, 0, 0])

# 计算模型得到的正确结果
true_positives = ((y_pred == y_true) & (y_pred == 1)).sum()
print(true_positives)
true_negatives = ((y_pred == y_true) & (y_pred == 0)).sum()
print(true_negatives)

# 计算模型得到的错误结果
false_positives = ((y_pred != y_true) & (y_pred == 1)).sum()
print(false_positives)
false_negatives = ((y_pred != y_true) & (y_pred == 0)).sum()
print(false_negatives)

# 误差矩阵或混淆矩阵
confusion = [[true_positives, false_positives],
                [false_negatives, true_negatives]]
print(confusion)
confusion = pd.DataFrame(confusion, columns=[1, 0], index=[1, 0])
confusion.index.name = r'pred \ truth'
print(confusion)

# 正确率
precision = true_positives / (true_positives + false_positives)
print(precision)

# 召回率也被称为灵敏度、真阳率或查全率
recall = true_positives / (true_positives + false_negatives)
print(recall)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值