使用scikit-learn计算AUC的正确方法是哪一种?

戴维·沃斯(David Ws。)

我注意到以下两个代码的结果是不同的。

#1
metrics.plot_roc_curve(classifier, X_test, y_test, ax=plt.gca())


#2
metrics.plot_roc_curve(classifier, X_test, y_test, ax=plt.gca(), label=clsname + ' (AUC = %.2f)' % roc_auc_score(y_test, y_predicted))

那么,哪种方法正确呢?

我添加了一个简单的可复制示例:

from sklearn.metrics import roc_auc_score
from sklearn import metrics
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.datasets import load_breast_cancer

data = load_breast_cancer()
X = data.data
y = data.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=12)

svclassifier = SVC(kernel='rbf')
svclassifier.fit(X_train, y_train)
y_predicted = svclassifier.predict(X_test)

print('AUC = %.2f' % roc_auc_score(y_test, y_predicted))  #1

metrics.plot_roc_curve(svclassifier, X_test, y_test, ax=plt.gca())  #2
plt.show()

输出(#1):

AUC = 0.86

而(#2):

在此处输入图片说明

Shijith

此处的差异可能是sklearn在内部predict_proba()用于获取每个类的概率,并从中找到auc

示例,当您使用 classifier.predict()

import matplotlib.pyplot as plt
from sklearn import datasets, metrics, model_selection, svm
X, y = datasets.make_classification(random_state=0)
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, random_state=0)
clf = svm.SVC(random_state=0,probability=False)
clf.fit(X_train, y_train)
clf.predict(X_test)

>> array([1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0,
       1, 0, 0])

# calculate auc
metrics.roc_auc_score(y_test, clf.predict(X_test))

>>>0.8782051282051283  # ~0.88

如果您使用 classifier.predict_proba()

X, y = datasets.make_classification(random_state=0)
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, random_state=0)
# set probability=True
clf = svm.SVC(random_state=0,probability=True)
clf.fit(X_train, y_train)
clf.predict_proba(X_test)

>> array([[0.13625954, 0.86374046],
       [0.90517034, 0.09482966],
       [0.19754525, 0.80245475],
       [0.96741274, 0.03258726],
       [0.80850602, 0.19149398],
       ......................,
       [0.31927198, 0.68072802],
       [0.8454472 , 0.1545528 ],
       [0.75919018, 0.24080982]])

# calculate auc
# when computing the roc auc metrics, by default, estimators.classes_[1] is   
# considered as the positive class here 'clf.predict_proba(X_test)[:,1]'

metrics.roc_auc_score(y_test, clf.predict_proba(X_test)[:,1])
>> 0.9102564102564102

因此对于您的问题,metrics.plot_roc_curve(classifier, X_test, y_test, ax=plt.gca())可能是使用默认值predict_proba()来预测auc,对于metrics.plot_roc_curve(classifier, X_test, y_test, ax=plt.gca(), label=clsname + ' (AUC = %.2f)' % roc_auc_score(y_test, y_predicted)),您正在计算roc_auc_score分数并将其作为标签传递。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Scikit-learn计算信息增益

Go方法,PutUint32或直接使用>>运算符是哪一种?

为了导入大数据(PHP),最好使用CSV或JSON中的哪一种

在scikit-learn中进行一种热编码的可能方法?

在Delphi中定义快捷方式的“正确”方法是哪一种?

ASP.NET MVC-应用程序预热-使用两种方法中的哪一种?

我应默认使用三种互斥的Clang消毒剂中的哪一种?

R:使用3D数组(纬度,经度和时间)随时间推移计算函数的最佳方法是哪一种?

哪一种是将String转换为ByteString的正确方法?

有没有一种方法可以使用scikit-learn“重新创建”数据?

这两种方法中的哪一种是在顶点/边缘存储数据的正确方法?

使用for和while循环编写素数函数的最Python方式是哪一种?

哪一种是正确的语法?

ADT的实例是哪一种?

将i18n与导轨上的许多html标签一起使用的正确或最佳方法是哪一种?

通常最好使用两种结构中的哪一种?

允许用户使用sudo命令有多种方式,哪一种更好?

以String格式获取Date哪一种比较经济?每次创建日期对象或通过日历对象使用日期实例

使用MDX查询检索数据的正确方法是哪一种?

哪一种是编写此gl代码的正确方法

无法使用类型为“ _”的参数列表调用“ _”-我应该使用两种选择中的哪一种?

将嵌套参数传递给构建方法的正确方法是哪一种?(Ruby on Rails 5)

哪一种最适合为 Xamarin.Forms 使用 Restful WebServices?

2 种创建对象的方法,不确定有什么区别或使用哪一种

这个 Dockerfile 语句有什么问题?我应该使用哪一种?

我应该使用哪一种?

我想确认这些计算 Dice Loss 的方法中哪一种是正确的

使用 cdn 或 npm 的 react 应用程序中哪一种字体很棒

为什么我的系统上有这么多 Python 实例?我的系统实际使用的是其中哪一种?