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
ENH Add parameter in colorbar in plot_confusion_matrix #17192
Conversation
Can anyone tell what what went wrong in this and how can I correct this? |
Hi @avigupta2612, your code have a linting issue.
Hope this can help. |
So this is my first PR. Plot_confusion_matrix tests are failing. So, do I need to edit the tests? If so, how can I do it? |
Tests related to each module of scikit-learn are in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @avigupta2612! Also, please add a new entry in the "what's new" file for version 0.24, describing your addition.
@@ -70,6 +71,7 @@ def plot(self, *, include_values=True, cmap='viridis', | |||
Axes object to plot on. If `None`, a new figure and axes is | |||
created. | |||
|
|||
colorbar : matplotlib colorbar, default=True | |||
Returns |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you check the rendering of the documentation you see that you need a blank line before the "Returns" paragraph in order to keep the correct indentation.
@@ -70,6 +71,7 @@ def plot(self, *, include_values=True, cmap='viridis', | |||
Axes object to plot on. If `None`, a new figure and axes is | |||
created. | |||
|
|||
colorbar : matplotlib colorbar, default=True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are adding a new parameter, a useful resource is the scikit-learn contributing documentation, in order to standardize the parameter description.
Also, could you please add a versionadded
directive? You can check #15426 for examples about how this should be addressed.
Thanks @cmarmo for reviewing. I have made some changes as you suggested, please have a look at them. |
Last comments on my side. Let's wait for other reviewers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Maybe check that by default the colorbar is not None?
Co-authored-by: Venkatachalam N <venky.yuvy@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we can improve the test mainly just to cover all possible use cases.
@@ -173,6 +173,9 @@ def test_confusion_matrix_display(pyplot, data, fitted_clf, y_pred, n_classes): | |||
image_data = disp.im_.get_array().data | |||
assert_allclose(image_data, cm) | |||
|
|||
disp.plot(colorbar=False) | |||
assert disp.im_.colorbar is None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we should add a separate test to check the effect of the parameter.
Indeed, it is appearing in disp.plot
and plot_confusion_matrix
so we should check that the behaviour is correct.
@pytest.mark.parametrize("colorbar, [True, False])
def test_plot_confusion_matrix_colorbar(colorbar):
disp = plot_confusion_matrix(..., colorbar=colorbar)
assert disp.im_.colorbar ...
# attempt a plot with the opposit effect
disp.plot(colorbar=not colorbar)
assert disp.im_.colorbar ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still needs to be addressed.
Hi @avigupta2612 , you already have one approval. Do you have some time to fix conflicts and apply last comments? Thanks for your work! |
@cmarmo I made the changes as per this 9fcc4d9 commit. |
@glemaitre or perhaps @thomasjpfan, all green here and one approval already. Do you mind having a look? Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR @avigupta2612 !
@@ -173,6 +173,9 @@ def test_confusion_matrix_display(pyplot, data, fitted_clf, y_pred, n_classes): | |||
image_data = disp.im_.get_array().data | |||
assert_allclose(image_data, cm) | |||
|
|||
disp.plot(colorbar=False) | |||
assert disp.im_.colorbar is None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still needs to be addressed.
I created a separate test for colorbar as suggestes by @glemaitre. I am running lint test and it is working fine but when I push the changes the tests are faling can you help here @cmarmo |
Some conflicts with upstream raised up, in the meanwhile. Merging with upstream (and solving conflicts), will solve your issues. |
doc/whats_new/v0.24.rst
Outdated
<<<<<<< HEAD | ||
======= |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those two lines should be removed: they are the relic of the automatic git diff with upstream
doc/whats_new/v0.24.rst
Outdated
:mod:`sklearn.datasets` | ||
....................... | ||
- |Feature| :func:`datasets.fetch_openml` now validates md5checksum of arff | ||
files downloaded or cached to ensure data integrity. | ||
:pr:`14800` by :user:`Shashank Singh <shashanksingh28>` and `Joel Nothman`_. | ||
|
||
>>>>>>> upstream/master |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here: to be removed.
doc/whats_new/v0.24.rst
Outdated
:mod:`sklearn.datasets` | ||
....................... | ||
- |Feature| :func:`datasets.fetch_openml` now validates md5checksum of arff | ||
files downloaded or cached to ensure data integrity. | ||
:pr:`14800` by :user:`Shashank Singh <shashanksingh28>` and `Joel Nothman`_. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed the entire block should be removed: it is already present on the top.
Modules are in alphabetical order in the what's new file.
@thomasjpfan all is green here but I did a mistake: the current what's new page contains two entries for the |
Right, if you still have some time do you mind re-adding it at the beginning of the file? Then I hope that you don't need to re-synchronize again before approval. Thanks for your patience. |
@cmarmo I guess this third entry for sklearn.datasets has been updated incorrectly in the main master branch here. Should I change it accoring to the master branch or should I include it in the sklearn.datasets section? |
I think the best option is keep it where it is in master and fix this in another pull request. Thanks.
You are very reactive and you know how to fix the issue, no need for that. :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 changes and we are good to go.
doc/whats_new/v0.24.rst
Outdated
@@ -230,12 +234,6 @@ Changelog | |||
:meth:`tree.DecisionTreeRegressor.fit`, and has not effect. | |||
:pr:`17614` by :user:`Juan Carlos Alfaro Jiménez <alfaro96>`. | |||
|
|||
:mod:`sklearn.datasets` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should reintroduce these lines (233-238) at the exact same position.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have included entry for my change at lines (145-148). So shouldn't I include skleanrn.datasets entry at lines 237-242?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These lines are red in the diff because you removed them (probably not on purpose but when fixing a conflict). Thus, you should reinsert such that we don't see any diff on GitHub (meaning that you did not edit the lines apart of your additional entry).
disp = plot_confusion_matrix(fitted_clf, X, y, colorbar=True) | ||
assert disp.im_.colorbar is not None | ||
|
||
# attempt a plot with the opposit effect | ||
disp.plot(colorbar=False) | ||
assert disp.im_.colorbar is None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
disp = plot_confusion_matrix(fitted_clf, X, y, colorbar=True) | |
assert disp.im_.colorbar is not None | |
# attempt a plot with the opposit effect | |
disp.plot(colorbar=False) | |
assert disp.im_.colorbar is None | |
def _check_colorbar(disp, has_colorbar): | |
if has_colorbar: | |
assert disp.im_.colorbar is not None | |
assert disp.im_.colorbar.__class__.__name__ == "Colorbar" | |
else: | |
assert disp.im_.colorbar is None | |
disp = plot_confusion_matrix(fitted_clf, X, y, colorbar=colorbar) | |
_check_colorbar(disp, colorbar) | |
# attempt a plot with the opposite effect of colorbar | |
disp.plot(colorbar=not colorbar) | |
_check_colorbar(disp, not colorbar) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to use the colorbar
created by the parametrization.
@glemaitre I have made all the changes as you suggested thanks for your help. |
@avigupta2612 Thanks for the work |
…17192) Co-authored-by: Venkatachalam N <venky.yuvy@gmail.com>
…17192) Co-authored-by: Venkatachalam N <venky.yuvy@gmail.com>
Reference Issues/PRs
Added feature to make colorbar optional as mentioned in #16519
What does this implement/fix? Explain your changes.
'
fig.colorbar(self.im_, ax=ax)
'
to
'
if self.colorbar:
fig.colorbar(self.im_, ax=ax)
'
Any other comments?