Skip to content

Suggestion: Remove prediction from plot_confusion_matrix and just pass predicted labels #15880

Closed

Description

@jhennrich

The signature of plot_confusion_matrix is currently:

sklearn.metrics.plot_confusion_matrix(estimator, X, y_true, labels=None, sample_weight=None, normalize=None, display_labels=None, include_values=True, xticks_rotation='horizontal', values_format=None, cmap='viridis', ax=None)

The function takes an estimator and raw data and can not be used with already predicted labels. This has some downsides:

  • If a confusion matrix should be plotted but the predictions should also be used elsewhere (e.g. calculating accuracy_score) the estimation has to be performed several times. That takes longer and can result in different values if the estimator is randomized.
  • If no estimator is available (e.g. predictions loaded from a file) the plot can not be used at all.

Suggestion: allow passing predicted labels y_pred to plot_confusion_matrix that will be used instead of estimator and X. In my opinion the cleanest solution would be to remove the prediction step from the function and use a signature similar to that of accuracy_score, e.g. (y_true, y_pred, labels=None, sample_weight=None, ...). However in order to maintain backwards compatibility, y_pred can be added as an optional keyword argument.

TODO:

Activity

  1. NicolasHug commented on Dec 13, 2019

    @NicolasHug
    Member

    We should definitely stay backward compatible, but adding a y_pred keyword arg sounds reasonable to me. We should raise an error if y_pred is passed but X or estimator are also passed.

    Would you want to submit a PR @jhennrich ?

  2. jhennrich commented on Dec 13, 2019

    @jhennrich
    Author

    I submitted a PR, but I think there is currently a problem with the CI so it has not passed yet.

  3. qinhanmin2014 commented on Dec 13, 2019

    @qinhanmin2014
    Member

    I agree that we should support plot_XXX(y_true, y_pred) to avoid calculating the prediction for multiple times.
    We also have similar issues in plot_roc_curve and plot_precision_recall_curve.
    Adding y_pred seems acceptable, but honestly I don't think it's a good solution.
    For those functions which accept **kwargs (e.g., plot_precision_recall_curve), seems that it's impossible to keep backward compatible?

  4. added this to the 0.22.1 milestone on Dec 13, 2019
  5. NicolasHug commented on Dec 13, 2019

    @NicolasHug
    Member

    Why is it impossible to keep the backward compatibility? It seems to me that the proposal in #15883 is OK

  6. qinhanmin2014 commented on Dec 13, 2019

    @qinhanmin2014
    Member

    Why is it impossible to keep the backward compatibility? It seems to me that the proposal in #15883 is OK

    because we do not support **kwargs in plot_confusion_matrix. @NicolasHug

  7. NicolasHug commented on Dec 13, 2019

    @NicolasHug
    Member

    Why is kwargs a problem?

  8. qinhanmin2014 commented on Dec 13, 2019

    @qinhanmin2014
    Member

    Hmm, so there's another annoying thing, we support **kwargs in plot_roc_curve and plot_precision_recall_curve (and plot_partial_dependence), but we do not support it in plot_confusion_matrix

  9. qinhanmin2014 commented on Dec 13, 2019

    @qinhanmin2014
    Member

    Why is kwargs a problem?

    if we add the new parameter before **kwargs, we can keep backward compatibility, right?

  10. jhennrich commented on Dec 13, 2019

    @jhennrich
    Author

    The changes in my PR are backwards compatible and **kwargs can still be added. But I agree with @qinhanmin2014, a much much cleaner solution would be to throw out estimator and X and use positional arguments (y_true, y_pred, ...) that are consistent with most of the other sklearn stuff.

  11. NicolasHug commented on Dec 13, 2019

    @NicolasHug
    Member

    if we add the new parameter before **kwargs, we can keep backward compatibility, right?

    yes

    a much much cleaner solution....

    Unfortunately that would require a deprecation cycle (unless we make it very fast in the bugfix release but I doubt it...)

    @thomasjpfan , any reason to pass the estimator as input instead of the predictions?

  12. qinhanmin2014 commented on Dec 13, 2019

    @qinhanmin2014
    Member

    Thanks, let's add y_pred first, **kwags is another issue.

  13. qinhanmin2014 commented on Dec 13, 2019

    @qinhanmin2014
    Member

    Unfortunately that would require a deprecation cycle (unless we make it very fast in the bugfix release but I doubt it...)

    This seems impossible, sigh

    @thomasjpfan , any reason to pass the estimator as input instead of the predictions?

    I agree that we need to reconsider our API design. also try to ping @amueller

  14. 51 remaining items

  15. modified the milestones: 0.24, 1.0 on Sep 17, 2020
  16. NicolasHug commented on Oct 3, 2020

    @NicolasHug
    Member

    Looks like there's no strong opposition to using 2 class methods, so let's do that. We'll need to:

    • Introduce the class methods for the currently existing plots:

      • ConfusionMatrixDisplay
      • PrecisionRecallDisplay
      • RocCurveDisplay
      • DetCurveDisplay
      • PartialDependenceDisplay. For this one, we don't want to introduce the from_predictions classmethod because it would not make sense, we only want from_estimator.
    • For all Display listed above, deprecate their corresponding plot_... function. We don't need to deprecate plot_det_curve because it hasn't been released yet, we can just remove it.

    • for new PRs like ENH Add CalibrationDisplay plotting class #17443 and FEA add PredictionErrorDisplay #18020 we can implement the class methods right away instead of introducing a plot function.

    This is a bit of work but I think we can get this done before 0.24 so that #17443 and #18020 can move forward already.

    Any objection @thomasjpfan @jnothman @amueller @glemaitre ?

    @jhennrich @pzelasko , would you be interested in submitting a PR to introduce the class methods in one of the Display objects?

  17. lucyleeow commented on Oct 3, 2020

    @lucyleeow
    Member

    Thanks for making the decision @NicolasHug! I'll get onto #17443 (after waiting for objections)

  18. thomasjpfan commented on Oct 5, 2020

    @thomasjpfan
    Member

    I have no objections.

  19. glemaitre commented on Oct 6, 2020

    @glemaitre
    Member

    No objection as well.

  20. glemaitre commented on Oct 6, 2020

    @glemaitre
    Member

    I will take care of the other classes then and advance my stalled PR.
    @lucyleeow in case I did not do all of those and you are searching for some PRs, ping me :)

  21. self-assigned this
    on Oct 6, 2020
  22. pzelasko commented on Oct 6, 2020

    @pzelasko

    I'd love to contribute but I'm engaged in too many projects at this time. Thanks for listening to the suggestions!

  23. amueller commented on Oct 7, 2020

    @amueller
    Member

    Sounds good :)

  24. adrinjalali commented on Aug 22, 2021

    @adrinjalali
    Member

    Seems like there's been good progress here. Removing the milestone, but pinging y'all in case you think something needs to get in before the release.

  25. removed this from the 1.0 milestone on Aug 22, 2021
  26. glemaitre commented on Aug 22, 2021

    @glemaitre
    Member
  27. thomasjpfan commented on Apr 15, 2022

    @thomasjpfan
    Member

    I am closing this issue because all the tasks in the original issue has been merged.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions