Skip to content

[MRG] Allow plot_confusion_matrix to be called on predicted labels - #15883

Closed
jhennrich wants to merge 7 commits into
scikit-learn:mainfrom
jhennrich:enhancement/plot_confusion_matrix
Closed

jhennrich wants to merge 7 commits into
scikit-learn:mainfrom
jhennrich:enhancement/plot_confusion_matrix

Conversation

@jhennrich

Copy link
Copy Markdown

Allow passing predicted labels y_pred to plot_confusion_matrix that will be used instead of estimator and X. In order to maintain backwards compatibility, y_pred is added as an optional keyword argument.
When both estimator and X aswell as y_pred are passed, a ValueError is raised since it is unclear which of both should be used.

For the discussion see #15880

@NicolasHug NicolasHug left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @jhennrich

Don't worry about CI failing for now, it's unrelated.

please add a few tests. We would also need a short sentence in the user guide (doc/modules/model_evaluation.rst) to document this feature

@qinhanmin2014

Copy link
Copy Markdown
Member

thanks, please apply similar changes to plot_roc_curve and plot_precision_recall_curve

@NicolasHug

Copy link
Copy Markdown
Member

Maybe we can make changes to the other functions in another PR

@jhennrich

Copy link
Copy Markdown
Author

@NicolasHug: I fixed everything you suggested and added tests.

@marctorsoc

marctorsoc commented Jan 23, 2020 •

Copy link
Copy Markdown
Contributor

Hi, what happened with this? it seems ready to be merged. What are the plans for ROC curve and others? I feel quite inefficient having to predict again if I want to plot the ROC curve

On a similar matter, what do you think in general about having just one function with a boolean to plot? wouldn't that simplify a lot the API?

confusion_matrix(y_true, y_pred)   ----->
                                             confusion_matrix(...., plot={True, False})
plot_confusion_metric(estimator, X, y_true) ---> 

I think this would be nice because maybe you want to plot the CM, ROC, etc, and also have it. For example, you wanna plot the ROC curve and choose the threshold

@thomasjpfan

Copy link
Copy Markdown
Member

@marctorrellas There is a discussion on #15880 going through if we want inflate the API of this function.

In summary, having two interfaces in one function feels like it will lead to an inflated API. The way to do this with already computed predictions would be to:

from sklearn.metrics import ConfusionMatrixDisplay

y_pred = est.predict(...)
confusion_matrix = confusion_matrix(y_true, y_pred)
display_labels = [...]

disp = ConfusionMatrixDisplay(confusion_matrix=confusion_matrix,
                              display_labels=display_labels).plot(...)

@marctorsoc

Copy link
Copy Markdown
Contributor

@marctorrellas There is a discussion on #15880 going through if we want inflate the API of this function.

In summary, having two interfaces in one function feels like it will lead to an inflated API. The way to do this with already computed predictions would be to:

from sklearn.metrics import ConfusionMatrixDisplay

y_pred = est.predict(...)
confusion_matrix = confusion_matrix(y_true, y_pred)
display_labels = [...]

disp = ConfusionMatrixDisplay(confusion_matrix=confusion_matrix,
                              display_labels=display_labels).plot(...)

thanks so much for your answer! didn't realise about those *****Display 👍

Base automatically changed from master to main January 22, 2021 10:51
@thomasjpfan

Copy link
Copy Markdown
Member

Now that ConfusionMatrix.from_predictions is released, we can now plot the confusion matrix from the predictions directly. With that in mind, I am closing this PR. Thank you for working on this PR!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants