Skip to content

Enhancement to Confusion Matrix Output Representation for improving readability #19012 #19190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

shubhamdo
Copy link

Reference Issues/PRs

Fixes #19012

What does this implement/fix? Explain your changes.

When you have multiple levels you can have difficulty reading the ndarray, associating the levels with the True and Predicted values. It is an enhancement to the output of confusion matrix function, better representing the true and predicted values for multilevel classes.

  • Returns a confusion matrix in dict representation with labels as keys ('true', 'pred')
  • Example:
    >>> y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
    >>> y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
    >>> confusion_matrix(y_true, y_pred, labels=["ant", "bird", "cat"], pprint=True)
    {('ant', 'ant'): 2, ('bird', 'ant'): 0, ('cat', 'ant'): 1,
     ('ant', 'bird'): 0, ('bird', 'bird'): 0, ('cat', 'bird'): 0,
     ('ant', 'cat'): 0, ('bird', 'cat'): 1, ('cat', 'cat'): 2}

Any other comments?

Copy link
Member

@jnothman jnothman left a comment

Choose a reason for hiding this comment

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

Thanks for the PR.

@@ -249,6 +249,10 @@ def confusion_matrix(y_true, y_pred, *, labels=None, sample_weight=None,
conditions or all the population. If None, confusion matrix will not be
normalized.

pprint : bool, default=False
Copy link
Member

Choose a reason for hiding this comment

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

let's call this as_dict?

@@ -257,6 +261,14 @@ def confusion_matrix(y_true, y_pred, *, labels=None, sample_weight=None,
samples with true label being i-th class
and predicted label being j-th class.

Or
Copy link
Member

Choose a reason for hiding this comment

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

This isn't valid numpydoc. The types need to be mentioned all on the first line.

Copy link
Author

@shubhamdo shubhamdo Jan 18, 2021

Choose a reason for hiding this comment

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

  1. Changed parameter name from pprint --> as_dict()
  2. Changed the Testing Function in test_classification.py
  3. Changed the Docstring for the function, added explaination of Series usage

Not sure about the numpydoc, I have changed it please review.

  • Or should I mention it as --> tuple[ndarry, dict['true_class','pred_class']] ?

@@ -249,6 +249,10 @@ def confusion_matrix(y_true, y_pred, *, labels=None, sample_weight=None,
conditions or all the population. If None, confusion matrix will not be
normalized.

pprint : bool, default=False
Returns a confusion matrix in dict representation with labels as keys
('true', 'pred')
Copy link
Member

Choose a reason for hiding this comment

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

It would be worth briefly noting the usage with pandas and unstack.

1. Changed parameter name from pprint --> as_dict()
2. Changed the Testing Function in test_classification.py
3. Tested
4. Changed the Docstring for the function, added explaination of Series usage
Base automatically changed from master to main January 22, 2021 10:53
@tansaku
Copy link

tansaku commented Nov 6, 2022

I guess this PR has stalled?

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.

Confusion Matrix Representation / Return Value
4 participants