使用roc_auc_score()和auc()得到不同的结果

我很难理解scikit-learn中roc_auc_score()auc()之间的区别(如果有的话)。

我试图预测一个二元输出,类别不平衡(Y=1的大约1.5%)。

分类器

model_logit = LogisticRegression(class_weight='auto')model_logit.fit(X_train_ridge, Y_train)

ROC曲线

false_positive_rate, true_positive_rate, thresholds = roc_curve(Y_test, clf.predict_proba(xtest)[:,1])

AUC的值

auc(false_positive_rate, true_positive_rate)Out[490]: 0.82338034042531527

roc_auc_score(Y_test, clf.predict(xtest))Out[493]: 0.75944737191205602

有人能解释这种差异吗?我以为两者只是在计算ROC曲线下的面积。可能是由于数据集不平衡,但我无法弄清楚为什么会这样。

谢谢!


回答:

AUC并不总是ROC曲线下的曲线下面积。曲线下面积是某个曲线下的(抽象)面积,因此它比AUROC普遍。对于类别不平衡的情况,可能更适合计算精确度-召回率曲线的AUC。

查看roc_auc_score的sklearn源码:

def roc_auc_score(y_true, y_score, average="macro", sample_weight=None):    # <...> docstring <...>    def _binary_roc_auc_score(y_true, y_score, sample_weight=None):            # <...> bla-bla <...>                fpr, tpr, tresholds = roc_curve(y_true, y_score,                                            sample_weight=sample_weight)            return auc(fpr, tpr, reorder=True)        return _average_binary_score(        _binary_roc_auc_score, y_true, y_score, average,        sample_weight=sample_weight) 

如你所见,这首先获取ROC曲线,然后调用auc()来获取面积。

我猜你的问题出在predict_proba()的调用上。对于普通的predict(),输出总是相同的:

import numpy as npfrom sklearn.linear_model import LogisticRegressionfrom sklearn.metrics import roc_curve, auc, roc_auc_scoreest = LogisticRegression(class_weight='auto')X = np.random.rand(10, 2)y = np.random.randint(2, size=10)est.fit(X, y)false_positive_rate, true_positive_rate, thresholds = roc_curve(y, est.predict(X))print auc(false_positive_rate, true_positive_rate)# 0.857142857143print roc_auc_score(y, est.predict(X))# 0.857142857143

如果你将上述代码改为以下内容,有时会得到不同的输出:

false_positive_rate, true_positive_rate, thresholds = roc_curve(y, est.predict_proba(X)[:,1])# 可能不同print auc(false_positive_rate, true_positive_rate)print roc_auc_score(y, est.predict(X))

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注