Skip to content

plot_roc_curve doesn't correctly infer pos_label #15303

Closed

Description

@amueller
from sklearn.metrics import plot_roc_curve
from sklearn.datasets import make_blobs
from sklearn.linear_model import LogisticRegression

import numpy as np
X, y = make_blobs(centers=2)
y = y.astype(np.str)
lr = LogisticRegression().fit(X, y)
plot_roc_curve(lr, X, y)
-> raise ValueError("Data is not binary and pos_label is not specified")

I would argue that pos_label=lr.classes_[1] is the right choice here.

cc @thomasjpfan

Activity

  1. qinhanmin2014 commented on Oct 21, 2019

    @qinhanmin2014
    Member

    Maybe we should drop parameter pos_label in plot_roc_curve, because we do not have parameter pos_label in roc_auc_score. In roc_auc_score, for binary y_true, y_score is supposed to be the score of the class with greater label.

  2. added this to the 0.22 milestone on Oct 21, 2019
  3. qinhanmin2014 commented on Oct 28, 2019

    @qinhanmin2014
    Member

    @amueller if you want to infer pos_label and allow users to specify pos_label manually, one possible solution is to define default pos_label=None as follows:
    when y_true is in {-1, 1} or {0, 1}, pos_labe is set to 1, otherwise pos_label is set to the greater label.

    This is consistent with brier_score_loss. Also see #10010 #13628

  4. qinhanmin2014 commented on Oct 28, 2019

    @qinhanmin2014
    Member

    ping @thomasjpfan please share your opinion since it's a blocker, thanks :)

  5. thomasjpfan commented on Oct 29, 2019

    @thomasjpfan
    Member

    In this case, since we have the trained estimator, we can infer the pos_label from est.classes_[1] as suggested above.

    If a user wants to pass another pos_label we can make sure that pos_label appears in est.classes_.

  6. ogrisel commented on Oct 30, 2019

    @ogrisel
    Member

    The current error message is confusing because the data is binary (but has ambiguous string labels). But I think that automatically inferring pos_label=est.classes_[1] is arbitrary for string labels.

    I think we should improve the error message but not try to automatically infer the positive class when we get string labels.

  7. ogrisel commented on Oct 30, 2019

    @ogrisel
    Member

    I am working on a PR.

  8. qinhanmin2014 commented on Oct 30, 2019

    @qinhanmin2014
    Member

    there's a PR :) #15316

  9. ogrisel commented on Oct 30, 2019

    @ogrisel
    Member

    there's a PR :) #15316

    Sorry I had not seen it.... My PR #15405 is taking a different stand on the issue: refusing to infer when there is ambiguity.

  10. jnothman commented on Oct 30, 2019

    @jnothman
    Member
  11. ogrisel commented on Nov 6, 2019

    @ogrisel
    Member

    Maybe instead of "true positive rate" and "false positive rate" we should
    label the axes "sensitivity to class 1" and "sensitivity to class 0" and
    similar for other class labels....

    Actually we should rather use the string class labels in the axis labels (rather than the implicitly encoded labels).

  12. jnothman commented on Nov 6, 2019

    @jnothman
    Member
  13. ogrisel commented on Nov 6, 2019

    @ogrisel
    Member

    FPR is not sensitivity to class 0 right? TNR would be sensitivity to class 0. (I think). I am not sure how we could improve the curve axis labels.

  14. qinhanmin2014 commented on Nov 6, 2019

    @qinhanmin2014
    Member

    @ogrisel @jnothman Actually I no longer regard this as a blocker. This is consistent with the repo if we think roc_auc_score is problematic (i.e., it should also support pos_label), because average_precision_score, roc_curve, precision_recall_curve do not infer pos_label automatically.

    import numpy as np
    from sklearn.datasets import load_iris
    from sklearn.linear_model import LogisticRegression
    from sklearn.model_selection import cross_val_score
    
    X, y = load_iris(return_X_y=True)
    X, y = X[y != 2], y[y != 2]
    clf = LogisticRegression()
    cross_val_score(clf, X, y, scoring="average_precision")
    # array([1., 1., 1., 1., 1.])
    
    y = np.array(["pos", "neg"])[y]
    clf = LogisticRegression()
    cross_val_score(clf, X, y, scoring="average_precision")
    # ValueError: pos_label=1 is invalid. Set it to a label in y_true.
    
  15. qinhanmin2014 commented on Nov 6, 2019

    @qinhanmin2014
    Member

    Perhaps we can simply use pos_label=1 as the default and never infer pos_label.

  16. ogrisel commented on Nov 6, 2019

    @ogrisel
    Member

    Perhaps we can simply use pos_label=1 as the default and never infer pos_label.

    I am actually thinking of the opposite: #15405 (comment)

  17. jnothman commented on Nov 6, 2019

    @jnothman
    Member
  18. amueller commented on Nov 6, 2019

    @amueller
    MemberAuthor

    See my comment in #15405 (comment) please. I was a bit confused about what is happening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions