sklearn auc ValueError:y_true中只有一个类

All*_*uin 12 scikit-learn

我搜索了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的行为不同.

Kri*_*ris 3

我认为你的预感是正确的。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)

  • 几乎可以肯定你已经解决了你的问题,但我最近遇到了同样的问题,结果发现原因很简单:cross_val_score默认使用_shuffling_交叉验证,理论上它有一个非零的机会为auc计算提供足够数量的两个类。我不知道你使用哪个分离器,但它可能是类似 TimeSeriesSplit 的东西。根据定义,这_不是_洗牌。从今以后,对于不平衡的数据集,这种分割_可以_为某些特定的折叠生成同一类的所有实例。 (3认同)