python auc_python – 与roc_auc_score()和auc()不同的结果

AUC并不等同于ROC曲线下的面积,尤其在处理不平衡数据集时。`roc_auc_score`函数通过计算ROC曲线下的面积实现评估。文章通过示例解释了`roc_curve`和`auc`函数的使用,并指出`predict_proba()`的不同调用方式可能会影响结果。
摘要由CSDN通过智能技术生成

AUC并不总是ROC曲线下的面积.曲线下面积是某个曲线下的(抽象)区域,因此它比AUROC更通用.对于不平衡类,最好找到精确回忆曲线的AUC.

请参阅sklearn source for roc_auc_score:

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()调用.对于正常的预测(),输出总是相同的:

import numpy as np

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import roc_curve, auc, roc_auc_score

est = 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.857142857143

print roc_auc_score(y, est.predict(X))

# 0.857142857143

如果您为此更改了上述内容,则有时会得到不同的输出:

false_positive_rate, true_positive_rate, thresholds = roc_curve(y, est.predict_proba(X)[:,1])

# may differ

print auc(false_positive_rate, true_positive_rate)

print roc_auc_score(y, est.predict(X))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值