分类模型的性能指标包括准确率(accuracy)、精确率(precision)、召回率(recall)、F1分数(F1_score)、ROC曲线(ROC_curve)、AUC(AUC)等。本题具体使用的性能指标如下
- 混淆矩阵(Confusion Matrix):
其中,TP是真阳性,TN是真阴性,FP是假阳性,FN是假阴性。
- 准确率(Accuracy):
- F1分数(F1 Score):
其中,Precision = ,Recall =
。
- 特异度(Specificity):
- 负预测值(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)