我搜索了谷歌,并看到了几个关于这个错误的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)
我想知道在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)
因此,它仍然使用roc_auc_score函数。我不明白为什么cross_val_score通过直接调用roc_auc_score来表现不同。
发布于 2016-08-19 19:56:18
我认为你的预感是对的。AUC (在ROC曲线下的面积)需要足够数量的任何一个类才有意义。
默认情况下,cross_val_score
分别计算性能度量指标1。另一种选择是做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)
https://stackoverflow.com/questions/39018097
复制相似问题