I am doing a k-fold XV on an existing dataframe, and I need to get the AUC score. The problem is - sometimes the test data only contains 0s, and not 1s!
I tried using this example, but with different numbers:
import numpy as np
from sklearn.metrics import roc_auc_score
y_true = np.array([0, 0, 0, 0])
y_scores = np.array([1, 0, 0, 0])
roc_auc_score(y_true, y_scores)
And I get this exception:
ValueError: Only one class present in y_true. ROC AUC score is not defined in that case.
Is there any workaround that can make it work in such cases?
I am facing the same problem now, and using
try-catch
does not solve my issue. I developed the code below in order to deal with that.I just wrote that code and I did not tested it exhaustively. It was tested only for binary categories. Hope it be useful yet.
Yes, it is clearly a bug! Your code is perfectly correct:
Here is my "fix"
You could use try-except to prevent the error:
Now you can also set the
roc_auc_score
to be zero if there is only one class present. However, I wouldn't do this. I guess your test data is highly unbalanced. I would suggest to use stratified K-fold instead so that you at least have both classes present.Simply modify the code with 0 to 1 make it work
I believe the error message has suggested that only one class in y_true (all zero), you need to give 2 classes in y_true.