Skip to content

FIX Avoid setting legend when labels are None in RocCurveDisplay kwargs #29727

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

Merged
merged 7 commits into from
Jan 13, 2025

Conversation

ArturoAmorQ
Copy link
Member

@ArturoAmorQ ArturoAmorQ commented Aug 27, 2024

Reference Issues/PRs

What does this implement/fix? Explain your changes.

In #29617 and #29611 the RocCurveDisplay is used to visualize the roc curve across several cross-validation splits. As having the labels for each split is unnecessary, we rather set label=None, but that rises a UserWarning: No artists with labels found to put in legend due to the legend being set at a lower level inside the display.

This PR fixes the issue.

Any other comments?

Notice that "label" in chance_level_line_kw and "label" in line_kwargs are always True, as those keys are assigned earlier in the code. As "label" cannot be removed, only updated to None, we rather query for it's value using the get method.

Copy link

github-actions bot commented Aug 27, 2024

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: ca58f82. Link to the linter CI: here

if "label" in line_kwargs or "label" in chance_level_line_kw:
if (
line_kwargs.get("label") is not None
and chance_level_line_kw.get("label") is not None
Copy link
Member

Choose a reason for hiding this comment

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

Isn't it an or and instead of an and? We need one of the two not being None to show the legend?

It also means that we don't have a test right now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I did change the logic so that the legend is not set as soon as either of the labels is None.

The reasoning is that I don't think anyone would set plot_chance_lvl=True and override chance_level_kw with {"label": None}, so that part could be a bit more flexible. WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

For me this is counter-intuitive to have no legend for the chance level curve with the following code:

In [3]: import matplotlib.pyplot as plt
   ...: from sklearn.datasets import make_classification
   ...: from sklearn.metrics import RocCurveDisplay
   ...: from sklearn.model_selection import train_test_split
   ...: from sklearn.svm import SVC
   ...: X, y = make_classification(random_state=0)
   ...: X_train, X_test, y_train, y_test = train_test_split(
   ...:     X, y, random_state=0)
   ...: clf = SVC(random_state=0).fit(X_train, y_train)
   ...: RocCurveDisplay.from_estimator(
   ...:    clf, X_test, y_test, label=None, plot_chance_level=True
   ...: )
   ...: plt.show()

If I really want to turn off the legend for the chance level then I can do it with the right params:

In [2]: import matplotlib.pyplot as plt
   ...: from sklearn.datasets import make_classification
   ...: from sklearn.metrics import RocCurveDisplay
   ...: from sklearn.model_selection import train_test_split
   ...: from sklearn.svm import SVC
   ...: X, y = make_classification(random_state=0)
   ...: X_train, X_test, y_train, y_test = train_test_split(
   ...:     X, y, random_state=0)
   ...: clf = SVC(random_state=0).fit(X_train, y_train)
   ...: RocCurveDisplay.from_estimator(
   ...:    clf, X_test, y_test, label=None, plot_chance_level=True, chance_level_kw={"label": None},
   ...: )
   ...: plt.show()

So to have this last behaviour you need to have a or operator and not a and operator.

@glemaitre glemaitre self-requested a review September 9, 2024 19:29
@glemaitre
Copy link
Member

We will need an additional test to check that the behaviour is the one that we want.

@glemaitre glemaitre removed their request for review October 17, 2024 08:57
@@ -193,7 +198,6 @@ def test_roc_curve_chance_level_line(
assert display.chance_level_.get_linestyle() == "--"
assert display.chance_level_.get_label() == "Chance level (AUC = 0.5)"
elif plot_chance_level:
assert display.chance_level_.get_label() == chance_level_kw["label"]
Copy link
Member Author

Choose a reason for hiding this comment

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

I removed this test as it is redundant with the assert chance_level_kw["label"] in legend_labels added further below.

@ArturoAmorQ
Copy link
Member Author

I addressed all your comments @glemaitre. Also I don't really think we need a changelog entry here, unless you think otherwise.

Copy link
Member

@glemaitre glemaitre left a 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 need to have an entry in a changelog because we change the behaviour and this is actually a fix.

Apart from that. LGTM.

@ArturoAmorQ ArturoAmorQ changed the title MAINT Avoid UserWarning when setting label=None in RocCurveDisplay line kwargs FIX Avoid UserWarning when setting label=None in RocCurveDisplay line kwargs Dec 9, 2024
Copy link
Member

@glemaitre glemaitre left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks @ArturoAmorQ

@ArturoAmorQ ArturoAmorQ changed the title FIX Avoid UserWarning when setting label=None in RocCurveDisplay line kwargs FIX Avoid setting legend when labels are None in RocCurveDisplay kwargs Dec 10, 2024
@ArturoAmorQ ArturoAmorQ added the Waiting for Second Reviewer First reviewer is done, need a second one! label Dec 20, 2024
Copy link
Contributor

@OmarManzoor OmarManzoor left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks @ArturoAmorQ

@OmarManzoor OmarManzoor merged commit 08eebc6 into scikit-learn:main Jan 13, 2025
35 checks passed
@ArturoAmorQ ArturoAmorQ deleted the line_kwargs branch January 13, 2025 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module:metrics Waiting for Second Reviewer First reviewer is done, need a second one!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants