roc_auc_score - Only one class present in y_true

前端 未结 5 1487
忘了有多久
忘了有多久 2020-12-16 14:28

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 usin

5条回答
  •  执念已碎
    2020-12-16 14:44

    You could use try-except to prevent the error:

    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])
    try:
        roc_auc_score(y_true, y_scores)
    except ValueError:
        pass
    

    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.

提交回复
热议问题