检测错误权衡曲线

Machine LearningMachine LearningBeginner
立即练习

This tutorial is from open-source community. Access the source code

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在本教程中,我们将了解检测错误权衡(DET)曲线,并将它们与接收者操作特征(ROC)曲线进行比较。DET曲线是ROC曲线的一种变体,其中y轴上绘制的是假阴性率(FNR),而不是真阳性率(TPR)。我们将使用scikit-learn(一个流行的用于机器学习的Python库)来生成合成数据,并使用ROC和DET曲线比较两个分类器在不同阈值下的统计性能。

虚拟机使用提示

虚拟机启动完成后,点击左上角切换到“笔记本”标签,以访问Jupyter Notebook进行练习。

有时,你可能需要等待几秒钟让Jupyter Notebook完成加载。由于Jupyter Notebook的限制,操作验证无法自动化。

如果你在学习过程中遇到问题,随时向Labby提问。课程结束后提供反馈,我们将立即为你解决问题。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL sklearn(("Sklearn")) -.-> sklearn/UtilitiesandDatasetsGroup(["Utilities and Datasets"]) ml(("Machine Learning")) -.-> ml/FrameworkandSoftwareGroup(["Framework and Software"]) sklearn(("Sklearn")) -.-> sklearn/CoreModelsandAlgorithmsGroup(["Core Models and Algorithms"]) sklearn(("Sklearn")) -.-> sklearn/DataPreprocessingandFeatureEngineeringGroup(["Data Preprocessing and Feature Engineering"]) sklearn(("Sklearn")) -.-> sklearn/ModelSelectionandEvaluationGroup(["Model Selection and Evaluation"]) sklearn/CoreModelsandAlgorithmsGroup -.-> sklearn/ensemble("Ensemble Methods") sklearn/CoreModelsandAlgorithmsGroup -.-> sklearn/svm("Support Vector Machines") sklearn/DataPreprocessingandFeatureEngineeringGroup -.-> sklearn/preprocessing("Preprocessing and Normalization") sklearn/DataPreprocessingandFeatureEngineeringGroup -.-> sklearn/pipeline("Pipeline") sklearn/ModelSelectionandEvaluationGroup -.-> sklearn/model_selection("Model Selection") sklearn/ModelSelectionandEvaluationGroup -.-> sklearn/metrics("Metrics") sklearn/UtilitiesandDatasetsGroup -.-> sklearn/datasets("Datasets") ml/FrameworkandSoftwareGroup -.-> ml/sklearn("scikit-learn") subgraph Lab Skills sklearn/ensemble -.-> lab-49103{{"检测错误权衡曲线"}} sklearn/svm -.-> lab-49103{{"检测错误权衡曲线"}} sklearn/preprocessing -.-> lab-49103{{"检测错误权衡曲线"}} sklearn/pipeline -.-> lab-49103{{"检测错误权衡曲线"}} sklearn/model_selection -.-> lab-49103{{"检测错误权衡曲线"}} sklearn/metrics -.-> lab-49103{{"检测错误权衡曲线"}} sklearn/datasets -.-> lab-49103{{"检测错误权衡曲线"}} ml/sklearn -.-> lab-49103{{"检测错误权衡曲线"}} end

生成合成数据

我们将使用scikit-learn的make_classification函数来生成合成数据。此函数会生成一个随机的n分类问题,具有n_informative个信息特征、n_redundant个冗余特征以及每个类别n_clusters_per_class个聚类。我们将生成1000个样本,有2个信息特征,随机状态设为1。然后,我们会以60/40的比例将数据拆分为训练集和测试集。

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

X, y = make_classification(
    n_samples=1_000,
    n_features=2,
    n_redundant=0,
    n_informative=2,
    random_state=1,
    n_clusters_per_class=1,
)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0)

定义分类器

我们将定义两个不同的分类器,以便使用ROC和DET曲线比较它们在不同阈值下的统计性能。我们将使用scikit-learn的make_pipeline函数创建一个管道,该管道使用StandardScaler对数据进行缩放,并训练一个LinearSVC分类器。我们还将使用scikit-learn的RandomForestClassifier类来训练一个随机森林分类器,其最大深度为5,有10个估计器,最多1个特征。

from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import make_pipeline
from sklearn.svm import LinearSVC

classifiers = {
    "Linear SVM": make_pipeline(StandardScaler(), LinearSVC(C=0.025, dual="auto")),
    "Random Forest": RandomForestClassifier(
        max_depth=5, n_estimators=10, max_features=1
    ),
}

绘制ROC和DET曲线

我们将分别使用scikit-learn的RocCurveDisplayDetCurveDisplay类来绘制ROC曲线和DET曲线。RocCurveDisplay.from_estimator函数计算ROC曲线并将其绘制在给定的轴上。类似地,DetCurveDisplay.from_estimator函数计算DET曲线并将其绘制在给定的轴上。我们将创建两个子图,一个用于ROC曲线,一个用于DET曲线,并为每个分类器绘制曲线。

import matplotlib.pyplot as plt
from sklearn.metrics import DetCurveDisplay, RocCurveDisplay

fig, [ax_roc, ax_det] = plt.subplots(1, 2, figsize=(11, 5))

for name, clf in classifiers.items():
    clf.fit(X_train, y_train)

    RocCurveDisplay.from_estimator(clf, X_test, y_test, ax=ax_roc, name=name)
    DetCurveDisplay.from_estimator(clf, X_test, y_test, ax=ax_det, name=name)

ax_roc.set_title("Receiver Operating Characteristic (ROC) curves")
ax_det.set_title("Detection Error Tradeoff (DET) curves")

ax_roc.grid(linestyle="--")
ax_det.grid(linestyle="--")

plt.legend()
plt.show()

结果解读

使用DET曲线比使用ROC曲线更容易直观地评估不同分类算法的整体性能。DET曲线直接给出了检测错误权衡的反馈,有助于进行操作点分析。然后,用户可以决定他们愿意接受的FNR,以牺牲FPR为代价(反之亦然)。

总结

在本教程中,我们了解了检测错误权衡(DET)曲线,并将它们与接收者操作特征(ROC)曲线进行了比较。我们使用scikit-learn生成合成数据,并使用ROC和DET曲线比较了两个分类器在不同阈值下的统计性能。DET曲线是ROC曲线的一种变体,其中y轴上绘制的是假阴性率(FNR)而不是真阳性率(TPR)。DET曲线直接给出了检测错误权衡的反馈,有助于进行操作点分析,使其成为评估分类算法性能的有用工具。