牛客题解 | 生成二元分类的混淆矩阵

题目

题目链接

混淆矩阵是一种描述分类模型性能的矩阵,其计算公式为:

[ T P F N F P T N ] \begin{bmatrix} TP & FN \\ FP & TN \end{bmatrix} [TPFPFNTN]

其中,TP是真阳性,TN是真阴性,FP是假阳性,FN是假阴性。
TP = True Positives (真正例): 正确预测为正类的样本数量
TN = True Negatives (真阴性): 正确预测为负类的样本数量
FP = False Positives (假阳性): 错误预测为正类的样本数量
FN = False Negatives (假阴性): 错误预测为负类的样本数量

标准代码如下

def confusion_matrix(data):
    # Count all occurrences
    counts = Counter(tuple(pair) for pair in data)
    # Get metrics
    TP, FN, FP, TN = counts[(1, 1)], counts[(1, 0)], counts[(0, 1)], counts[(0, 0)]
    # Define matrix and return
    confusion_matrix = [[TP, FN], [FP, TN]]
    return confusion_matrix
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值