牛客题解 | 计算分类模型的性能指标

题目

题目链接

分类模型的性能指标包括准确率(accuracy)、精确率(precision)、召回率(recall)、F1分数(F1_score)、ROC曲线(ROC_curve)、AUC(AUC)等。本题具体使用的性能指标如下

  1. 混淆矩阵(Confusion Matrix):
    [ T P F N F P T N ] \begin{bmatrix} TP & FN \\ FP & TN \end{bmatrix} [TPFPFNTN]

其中,TP是真阳性,TN是真阴性,FP是假阳性,FN是假阴性。
T P = 真正例(True Positive) = 被正确分类为正类的样本数 TP = \text{真正例(True Positive)} = \text{被正确分类为正类的样本数} TP=真正例(True Positive=被正确分类为正类的样本数
T N = 真负例(True Negative) = 被正确分类为负类的样本数 TN = \text{真负例(True Negative)} = \text{被正确分类为负类的样本数} TN=真负例(True Negative=被正确分类为负类的样本数
F P = 假阳性(False Positive) = 被错误分类为正类的负类样本数 FP = \text{假阳性(False Positive)} = \text{被错误分类为正类的负类样本数} FP=假阳性(False Positive=被错误分类为正类的负类样本数
F N = 假阴性(False Negative) = 被错误分类为负类的正类样本数 FN = \text{假阴性(False Negative)} = \text{被错误分类为负类的正类样本数} FN=假阴性(False Negative=被错误分类为负类的正类样本数

  1. 准确率(Accuracy):
    A c c u r a c y = T P + T N T P + T N + F P + F N Accuracy = \frac{TP + TN}{TP + TN + FP + FN} Accuracy=TP+TN+FP+FNTP+TN

  2. F1分数(F1 Score):
    F 1 = 2 ⋅ P r e c i s i o n ⋅ R e c a l l P r e c i s i o n + R e c a l l F1 = 2 \cdot \frac{Precision \cdot Recall}{Precision + Recall} F1=2Precision+RecallPrecisionRecall
    其中,Precision = T P T P + F P \frac{TP}{TP + FP} TP+FPTP,Recall = T P T P + F N \frac{TP}{TP + FN} TP+FNTP

  3. 特异度(Specificity):
    S p e c i f i c i t y = T N T N + F P Specificity = \frac{TN}{TN + FP} Specificity=TN+FPTN

  4. 负预测值(Negative Predictive Value):
    N e g a t i v e   P r e d i c t i v e   V a l u e = T N T N + F N Negative\ Predictive\ Value = \frac{TN}{TN + FN} Negative Predictive Value=TN+FNTN

标准代码如下

from collections import Counter 
def performance_metrics(actual: list[int], predicted: list[int]) -> tuple:
    data = list(zip(actual, predicted))
    counts = Counter(tuple(pair) for pair in data)
    TP, FN, FP, TN = counts[(1, 1)], counts[(1, 0)], counts[(0, 1)], counts[(0, 0)]
    confusion_matrix = [[TP, FN], [FP, TN]]
    accuracy = (TP + TN) / (TP + TN + FP + FN)
    precision = TP / (TP + FP)
    recall = TP / (TP + FN)
    f1 = 2 * precision * recall / (precision + recall)
    negativePredictive = TN / (TN + FN)
    specificity = TN / (TN + FP)
    return confusion_matrix, round(accuracy, 3), round(f1, 3), round(specificity, 3), round(negativePredictive, 3)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值