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

计算分类模型的性能指标

https://www.nowcoder.com/practice/942e318b00ad45cdb7a88f0136efdf21?tpId=377&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj

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

  1. 混淆矩阵(Confusion Matrix):

其中,TP是真阳性,TN是真阴性,FP是假阳性,FN是假阴性。

  1. 准确率(Accuracy):
  1. F1分数(F1 Score):

其中,Precision = ,Recall =

  1. 特异度(Specificity):
  1. 负预测值(Negative Predictive Value):

标准代码如下

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)
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务