题目
分类模型的性能指标包括准确率(accuracy)、精确率(precision)、召回率(recall)、F1分数(F1_score)、ROC曲线(ROC_curve)、AUC(AUC)等。本题具体使用的性能指标如下
- 混淆矩阵(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)=被错误分类为负类的正类样本数
-
准确率(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 -
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=2⋅Precision+RecallPrecision⋅Recall
其中,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。 -
特异度(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 -
负预测值(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)
1049

被折叠的 条评论
为什么被折叠?



