sklearn auc ValueError: Only one class present in y_true

前端 未结 1 662
长情又很酷
长情又很酷 2021-01-01 23:21

I searched Google, and saw a couple of StackOverflow posts about this error. They are not my cases.

I use keras to train a simple neural network and make some predi

相关标签:
1条回答
  • 2021-01-02 00:07

    I think your hunch is correct. The AUC (area under ROC curve) needs a sufficient number of either classes in order to make sense.

    By default, cross_val_score calculates the performance metric one each fold separately. Another option could be to do cross_val_predict and compute the AUC over all folds combined.

    You could do something like:

    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)
    
    0 讨论(0)
提交回复
热议问题