二分类任务的性能评估指标之 FPR、TPR、Recall 和 AUC¶
在机器学习的二分类任务中,常用的评估指标有 FPR(假阳性率)、TPR(真正率,也叫 Recall)以及 AUC(曲线下面积)。本文将简要介绍这些指标的定义,并展示如何使用 Python 计算这些指标,并绘制 ROC 曲线。
FPR, TPR 和 Recall 的定义¶
混淆矩阵¶
首先,我们定义 混淆矩阵(Confusion Matrix),它展示了分类模型的预测结果与实际标签之间的关系。混淆矩阵的每个位置表示特定类型的预测结果。
预测为正类 (Positive) | 预测为负类 (Negative) | |
---|---|---|
真实为正类 (Positive) | TP (True Positive) | FN (False Negative) |
真实为负类 (Negative) | FP (False Positive) | TN (True Negative) |
- TP (True Positive):真实为正类且预测为正类的样本数量。
- FN (False Negative):真实为正类但预测为负类的样本数量。
- FP (False Positive):真实为负类但预测为正类的样本数量。
- TN (True Negative):真实为负类且预测为负类的样本数量。
FPR (False Positive Rate):¶
假阳性率,表示模型将负类样本错误分类为正类的比例。这个比例越低越好。
TPR (True Positive Rate) / Recall:¶
真正率,表示模型正确识别的正类样本的比例。TPR 和 Recall 召回率是同一个概念。这个比例越高越好。
ROC 曲线和 AUC¶
ROC 曲线¶
ROC(Receiver Operating Characteristic)曲线是通过绘制 FPR(横轴)和 TPR(纵轴)来评估分类模型性能的工具。它展示了模型在不同阈值下的表现,帮助我们理解模型对正类和负类的分类能力。
AUC¶
AUC(Area Under Curve)就是 ROC 曲线下的面积,它是一个 0 到 1 之间的值,反映了模型的综合表现。AUC 的值越大,模型的表现越好。AUC 为 0.5 时,表示模型的表现相当于随机猜测。
示例代码¶
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import auc, roc_curve
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification
X, y = make_classification(
n_samples=1000,
n_features=20,
n_informative=2,
n_redundant=10,
n_clusters_per_class=2,
class_sep=0.5,
flip_y=0.1,
random_state=42,
)
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42
)
# 训练模型
model = LogisticRegression()
model.fit(X_train, y_train)
# 预测概率
y_score = model.predict_proba(X_test)[:, 1] # 取正类的概率
# 计算 FPR 和 TPR
fpr, tpr, thresholds = roc_curve(y_test, y_score)
# 计算 AUC
roc_auc = auc(fpr, tpr)
roc_df = pd.DataFrame(
{
"thresholds": thresholds,
"FPR": fpr,
"TPR": tpr,
}
)
display(roc_df)
# 绘制 ROC 曲线
plt.figure()
plt.plot(fpr, tpr, color="b", lw=2, label="ROC curve (area = %0.2f)" % roc_auc)
plt.plot([0, 1], [0, 1], color="gray", linestyle="--")
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")
plt.title("Receiver Operating Characteristic (ROC) Curve")
plt.legend(loc="lower right")
plt.show()
理解 ROC 曲线
对于一个二分类问题,在给定预测概率的情况下,不同的阈值会有不同的预测结果。
当阈值为 0 时,所有的样本都被预测为负例,此时对应 ROC 图中的左下角,也就是坐标轴的原点。当阈值逐渐下降时,越来越多的样本会被预测为正例,因此 True Positive Rate 会不变或上升,False Positive Rate 也会不变或上升。最终当阈值为 1 时,所有的样本都被预测为正例,此时对应 ROC 图中的右上角,也就是坐标轴的 \((1, 1)\)。
ROC 曲线是(非严格)单调递增的。当阈值的变化幅度足够小时,ROC 曲线就会比较光滑,它下方的面积就是 AUC。