gow*_*oww 35 python machine-learning scikit-learn
我无法理解scikit-learn 之间roc_auc_score()和之间的区别(如果有的话)auc().
我想用不平衡的类来预测二进制输出(Y = 1时约为1.5%).
model_logit = LogisticRegression(class_weight='auto')
model_logit.fit(X_train_ridge, Y_train)
Run Code Online (Sandbox Code Playgroud)
false_positive_rate, true_positive_rate, thresholds = roc_curve(Y_test, clf.predict_proba(xtest)[:,1])
Run Code Online (Sandbox Code Playgroud)
auc(false_positive_rate, true_positive_rate)
Out[490]: 0.82338034042531527
Run Code Online (Sandbox Code Playgroud)
和
roc_auc_score(Y_test, clf.predict(xtest))
Out[493]: 0.75944737191205602
Run Code Online (Sandbox Code Playgroud)
有人可以解释这个区别吗?我以为两者都只计算ROC曲线下的面积.可能是因为数据集不平衡但我无法弄清楚原因.
谢谢!
oop*_*ode 32
AUC并不总是ROC曲线下的面积.曲线下面积为下(抽象)地区的一些曲线,所以它比AUROC更一般的事情.对于不平衡类,最好找到精确回忆曲线的AUC.
请参阅sklearn来源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)
Run Code Online (Sandbox Code Playgroud)
如您所见,这首先获得roc曲线,然后调用auc()以获取该区域.
我想你的问题就是predict_proba()电话.对于正常情况predict(),输出始终相同:
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
Run Code Online (Sandbox Code Playgroud)
如果你为此更改了上述内容,有时会得到不同的输出:
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))
Run Code Online (Sandbox Code Playgroud)
And*_*eus 17
predict只返回一个类或另一个类.然后你用predict分类器的结果计算一个ROC,只有三个阈值(试验所有一个类,所有其他类都是平凡的,在它们之间).您的ROC曲线如下所示:
..............................
|
|
|
......|
|
|
|
|
|
|
|
|
|
|
|
Run Code Online (Sandbox Code Playgroud)
同时,predict_proba()返回整个概率范围,因此现在您可以在数据上设置三个以上的阈值.
.......................
|
|
|
...|
|
|
.....|
|
|
....|
.|
|
|
|
|
Run Code Online (Sandbox Code Playgroud)
因此不同的领域.
使用y_pred(类标签)时,您已经确定了阈值。当您使用y_prob(正类概率)时,您可以使用阈值,并且ROC曲线应该可以帮助您确定阈值。
对于第一种情况,您使用的是概率:
y_probs = clf.predict_proba(xtest)[:,1]
fp_rate, tp_rate, thresholds = roc_curve(y_true, y_probs)
auc(fp_rate, tp_rate)
Run Code Online (Sandbox Code Playgroud)
当您执行此操作时,您正在考虑“先于” AUC,然后再决定要使用的阈值。
在第二种情况下,您使用的是预测(而不是概率),在这种情况下,对两者都使用“ predict”而不是“ predict_proba”,您应该获得相同的结果。
y_pred = clf.predict(xtest)
fp_rate, tp_rate, thresholds = roc_curve(y_true, y_pred)
print auc(fp_rate, tp_rate)
# 0.857142857143
print roc_auc_score(y, y_pred)
# 0.857142857143
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39232 次 |
| 最近记录: |