混淆矩阵是一种描述分类模型性能的矩阵,其计算公式为:
其中,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