-
-
Notifications
You must be signed in to change notification settings - Fork 26.6k
FIX: Regression in DecisionBoundaryDisplay.from_estimator with colors and plot_method='contour' #31553
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
Conversation
adrinjalali
left a comment
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 for the PR. A few observations:
- The places which are not tested (complaining codecov) need to be tested.
- The diff is hard to review, and it seems certain logic is removed from the new code, which should be there? Please comment on how things have changed and if any, why logic is removed.
| "Cannot specify both 'cmap' and 'colors' in kwargs. " | ||
| "Please use only one of them." |
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.
isn't one deprecated upstream? We should recommend the new way.
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 for the suggestion!
To clarify: matplotlib hasn't deprecated either cmap or colors — both are still valid and serve different purposes.
That said, I agree it's better to recommend cmap as the preferred option, especially for continuous or gradient-based plots. I’ll update the error message accordingly to reflect that.
|
Thanks for the feedback! I understand that the diff is quite large and thus harder to review. After attempting to resolve the conflicts between And to clarify: matplotlib hasn't deprecated either cmap or colors — both are still valid and serve different purposes. That said, I agree it's better to recommend cmap as the preferred option, especially for continuous or gradient-based plots. Below is a breakdown of the changes for clarity: Original Logicif (
isinstance(self.multiclass_colors, str)
or self.multiclass_colors is None
):
...
if cmap == "tab10" and n_responses <= 10:
colors = ...
elif cmap == "tab20" and n_responses <= 20:
colors = ...
else:
colors = ...
elif isinstance(self.multiclass_colors, str):
colors = ...
else: # self.multiclass_colors is a list
colors = ...
Updated Logic (New Version):if self.multiclass_colors is None:
if "cmap" in kwargs and "colors" in kwargs:
raise ValueError(...)
if "cmap" in kwargs:
...
elif "colors" in kwargs:
...
else:
...
else:
if "cmap" in kwargs:
warnings.warn(...) # 'cmap' will be ignored
if "colors" in kwargs:
warnings.warn(...) # 'colors' will be ignored
if isinstance(self.multiclass_colors, str):
...
elif isinstance(self.multiclass_colors, list):
...
else:
raise ValueError(...)This new structure separates the logic into two clear branches:
This design avoids ambiguity, enforces correct usage, and adds necessary warnings and error handling. Contour-specific FixThe actual fix related to the contour plotting method is limited to lines 267 to 271, specifically in the following section: if plot_method == "contour":
# Plot only argmax map for contour
class_map = self.response.argmax(axis=2)
self.surface_ = plot_func(
self.xx0, self.xx1, class_map, colors=colors, **kwargs
)Regarding tests – I will add appropriate test cases to ensure the new logic is properly covered and behaves as expected in all scenarios. |
jeremiedbb
left a comment
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 for the PR @jshn9515. I made a first pass. Overall I find the new organization of the code to be an improvement. I think that some the added logic can be simplified though, see my comments below.
jeremiedbb
left a comment
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 played with it a bit and it mostly looks good. I just found that the tab10/tab20 special casing was removed, so I put it back because it gives better visualization (more distinguishable colors). I directly pushed the change since I had already done it locally.
Please add a changelog entry, mentioning the fix for contour, the fix for cmap and colors, and the fix for linear segmented cmaps.
LGTM once the changelog entry is added. Thanks @jshn9515 !
doc/whats_new/upcoming_changes/sklearn.inspection/31553.fix.rst
Outdated
Show resolved
Hide resolved
doc/whats_new/upcoming_changes/sklearn.inspection/31553.fix.rst
Outdated
Show resolved
Hide resolved
Co-authored-by: Jérémie du Boisberranger <[email protected]>
Co-authored-by: Jérémie du Boisberranger <[email protected]>
…colors' Co-authored-by: Jérémie du Boisberranger <[email protected]>
jeremiedbb
left a comment
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.
LGTM. Thanks @jshn9515 !
|
Thanks for the reviews and suggestions, @jeremiedbb and @adrinjalali! |
… and plot_method='contour' (scikit-learn#31553) Co-authored-by: Jérémie du Boisberranger <[email protected]>
… and plot_method='contour' (scikit-learn#31553) Co-authored-by: Jérémie du Boisberranger <[email protected]>
… and plot_method='contour' (scikit-learn#31553) Co-authored-by: Jérémie du Boisberranger <[email protected]>
… and plot_method='contour' (scikit-learn#31553) Co-authored-by: Jérémie du Boisberranger <[email protected]>
… and plot_method='contour' (scikit-learn#31553) Co-authored-by: Jérémie du Boisberranger <[email protected]>
… and plot_method='contour' (scikit-learn#31553) Co-authored-by: Jérémie du Boisberranger <[email protected]>
… and plot_method='contour' (scikit-learn#31553) Co-authored-by: Jérémie du Boisberranger <[email protected]>
Reference Issues/PRs
Fixes: #31546
Summary
This PR addresses bugs introduced in PR #29797. It restores the previous behavior when plot_method='contour' is specified and improves the handling of
colors,cmap, andmulticlass_colorsarguments in DecisionBoundaryDisplay.from_estimator.Problem
Currently, passing the
colorskeyword argument toDecisionBoundaryDisplay.from_estimatorraises aValueError, as bothcmapandcolorsare set simultaneously. Additionally, when plot_method='contour' is used, the decision boundary is no longer displayed; instead, a surface plot of the class predictions is drawn, which deviates from the intended behavior.Solution
This PR introduces the following enhancements to clarify and handle these scenarios:
multiclass_colorsis None:colorsandcmapare specified -> raise a ValueErrorcmapis specified -> determine whether the colormap is continuous or discrete and convert it to RGBA accordinglycolorsis specified -> convert to RGBAcmapnorcolorsis specified -> use the default colormapmulticlass_colorsis not None:cmaporcolorsis specified -> issue a warning and only usemulticlass_colorsmulticlass_colorsis a string -> treat it as a colormap name and convert it to RGBA depending on whether it's continuous or discretemulticlass_colorsis a list -> convert it directly to RGBADecisionBoundaryDisplay#29797 behavior)