我搜索了Google,看到了一些有关此错误的StackOverflow帖子.他们不是我的情况.
我使用keras训练一个简单的神经网络,并对分裂的测试数据集做出一些预测.但是当用于roc_auc_score计算AUC时,我得到以下错误:
"ValueError: Only one class present in y_true. ROC AUC score is not defined in that case.".
我检查目标标签分布,它们是高度不平衡的.一些标签(总共29个标签中)只有一个实例.因此,它们可能在测试标签中没有正面标签实例.所以sklearn的roc_auc_score函数报告了唯一的一个类问题.那是合理的.
但我很好奇,因为当我使用sklearn的cross_val_score功能时,它可以毫无错误地处理AUC计算.
my_metric = 'roc_auc'
scores = cross_validation.cross_val_score(myestimator, data,
labels, cv=5,scoring=my_metric)
Run Code Online (Sandbox Code Playgroud)
我不知道发生了什么cross_val_score,是不是因为cross_val_score使用了分层的交叉验证数据?
更新
我继续做一些挖掘,但仍然无法找到背后的差异.我看到cross_val_score调用check_scoring(estimator, scoring=None, allow_none=False)返回一个得分手,check_scoring并将调用get_scorer(scoring)将返回scorer=SCORERS[scoring]
而SCORERS['roc_auc']为roc_auc_scorer;
在roc_auc_scorer被由
roc_auc_scorer = make_scorer(roc_auc_score, greater_is_better=True,
needs_threshold=True)
Run Code Online (Sandbox Code Playgroud)
所以,它仍然使用roc_auc_score函数.我不明白为什么cross_val_score与直接调用roc_auc_score的行为不同.
我认为你的预感是正确的。AUC(ROC 曲线下面积)需要足够数量的任一类别才能有意义。
默认情况下,cross_val_score每折叠一次单独计算性能指标。另一种选择是计算cross_val_predict所有折叠的 AUC。
你可以这样做:
from sklearn.metrics import roc_auc_score
from sklearn.cross_validation import cross_val_predict
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
class ProbaEstimator(LogisticRegression):
"""
This little hack needed, because `cross_val_predict`
uses `estimator.predict(X)` internally.
Replace `LogisticRegression` with whatever classifier you like.
"""
def predict(self, X):
return super(self.__class__, self).predict_proba(X)[:, 1]
# some example data
X, y = make_classification()
# define your estimator
estimator = ProbaEstimator()
# get predictions
pred = cross_val_predict(estimator, X, y, cv=5)
# compute AUC score
roc_auc_score(y, pred)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10813 次 |
| 最近记录: |