question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion: Remove prediction from plot_confusion_matrix and just pass predicted labels

See original GitHub issue

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:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:64 (57 by maintainers)

github_iconTop GitHub Comments

2reactions
jnothmancommented, Dec 16, 2019

One classic pattern for this kind of thing is defining:

ConfusionMatrixDisplay.from_estimator(...)
ConfusionMatrixDisplay.from_predictions(...)

but this is not very idiomatic to scikit-learn.

1reaction
glemaitrecommented, Oct 6, 2020

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 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

sklearn plot confusion matrix with labels - Stack Overflow
In scikit-learn 0.22, there's a new feature to plot the confusion matrix directly (which, however, is deprecated in 1.0 and will be removed...
Read more >
Confusion Matrix Visualization - Medium
The confusion matrix is a 2 dimensional array comparing predicted category labels to the true label. For binary classification, these are ...
Read more >
sklearn.metrics.ConfusionMatrixDisplay
Plot the confusion matrix given an estimator, the data, and the label. ConfusionMatrixDisplay.from_predictions. Plot the confusion matrix given the true and ...
Read more >
Evaluating Multi-label Classifiers - Towards Data Science
A confusion matrix is a matrix that breaks down correctly and incorrectly classified into: True positive (TP): Correctly predicting the positive ...
Read more >
How to Make Predictions with Keras - Machine Learning Mastery
We can predict the class for new data instances using our finalized classification model in Keras using the predict_classes() function. Note ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found