📜  混淆矩阵python的精度和召回率(1)

📅  最后修改于: 2023-12-03 15:11:06.941000             🧑  作者: Mango

混淆矩阵(Confusion Matrix)及Python的精度和召回率

简介

混淆矩阵是一种可视化工具,用于衡量模型的性能。它通常用于分类问题,特别是二元分类问题。混淆矩阵将模型根据真实和预测的类别区分为四个不同的组:真正(True Positive)、假正(False Positive)、真负(True Negative)和假负(False Negative)。这些值可以用于计算模型的精度和召回率。

精度和召回率

**精度(Precision)**衡量的是我们预测为正类中,实际上也是正类的比例,即:

$ Precision = \frac{TP}{TP + FP}$

其中,TP表示真正(True Positive),FP表示假正(False Positive)。

**召回率(Recall)**衡量我们的模型在实际上是正类的样本中,正确标记出正类样本的能力,即:

$ Recall = \frac{TP}{TP + FN}$

其中,TP表示真正(True Positive),FN表示假负(False Negative)。

既然有了精度和召回率,我们就可以计算出一些其他的指标了。

F1分数(F1 Score):可以看作是精度和召回率的调和平均,它的值介于0和1之间。

$ F1Score = \frac{2 * Precision * Recall}{Precision + Recall}$

Python实现

在Python中,我们可以使用scikit-learn库来实现混淆矩阵和相关指标的计算。下面是一个简单的例子:

from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score

# 定义真实和预测的结果
y_true = [1, 1, 0, 1, 0, 0, 1, 0, 0, 1]
y_pred = [1, 0, 1, 1, 0, 0, 0, 1, 0, 1]

# 计算混淆矩阵
confusion_mat = confusion_matrix(y_true, y_pred)
print("Confusion Matrix:")
print(confusion_mat)

# 计算精度
precision = precision_score(y_true, y_pred)
print("Precision: {:.2f}".format(precision))

# 计算召回率
recall = recall_score(y_true, y_pred)
print("Recall: {:.2f}".format(recall))

# 计算F1分数
f1 = f1_score(y_true, y_pred)
print("F1 Score: {:.2f}".format(f1))

输出结果:

Confusion Matrix:
[[3 1]
 [2 4]]
Precision: 0.80
Recall: 0.67
F1 Score: 0.73

上面的代码中,我们使用了confusion_matrix()函数计算混淆矩阵。precision_score()、recall_score()和f1_score()函数用于计算精度、召回率和F1分数。它们都需要传入真实值(y_true)和预测值(y_pred)。在这个例子中,预测值为二元分类问题的结果(0或1),真实值为它们的标签。