FIX `predict` to also use `multiclass_colors` in DecisionBoundaryDisplay by AnneBeyer · Pull Request #33015 · scikit-learn/scikit-learn · GitHub
Skip to content

FIX predict to also use multiclass_colors in DecisionBoundaryDisplay - #33015

Merged
lucyleeow merged 32 commits into
scikit-learn:mainfrom
AnneBeyer:multiclass_colors_DecisionBoundaryDisplay
Feb 2, 2026
Merged

lucyleeow merged 32 commits into
scikit-learn:mainfrom
AnneBeyer:multiclass_colors_DecisionBoundaryDisplay

Conversation

@AnneBeyer

@AnneBeyer AnneBeyer commented Jan 6, 2026

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #32872 and the predict part of #32866

What does this implement/fix? Explain your changes.

Previously, the case distinction for binary vs. multiclass was made based on the response dimension (see #32918 for reference), which led to multiclass predict ignoring the multiclass_colors parameter.
The colors are now selected based on the n_classes display class variable, which is extracted in from_estimator.

Note that a distinction based on the response shape still has to be made for plotting. in two cases (contour and pcolormesh). I'm not sure if there is a more elegant solution, but I didn't find a way to handle pcolormesh in the same way for predict as in the other cases.

I adapted the docstrings and the exmple and added the response functions to the tests (and adapted them accordingly).

EDIT: The output of the code in issue #32872 now looks like this (Note that the levels problem from #32866 is still there for predict, but this PR is only concerned with making it use the same color map as the other methods, which it does now):
image

Since every class is plotted separately now (as it was already for response method predict_proba and decision_function), this also fixes the levels issue noted in #32866. However, there are white lines between the classes for predict with countourf. If this is a problem (and using pcolormesh is not an option for any reason), the current approach will need to be adapted further.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Any other comments?

I also added a test for the default value for multiclass_colors, which was missing.

@AnneBeyer
AnneBeyer marked this pull request as draft January 6, 2026 15:34
@ogrisel

ogrisel commented Jan 7, 2026

Copy link
Copy Markdown
Member

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I didn't check the details but from a cursory glance this looks great. Please feel free to ping me once CI is green.

I think it would be great to also find a way to include a non regression test for #32866, that is, that different regions are assigned distinct colors when n_classes is large.

EDIT: maybe inspecting the levels would be a way to achieve this, for instance by including/adapting the test of #32866.

Comment thread sklearn/inspection/_plot/tests/test_boundary_decision_display.py Outdated
@github-project-automation github-project-automation Bot moved this to Todo in Labs Jan 8, 2026
@StefanieSenger StefanieSenger moved this from Todo to In progress in Labs Jan 8, 2026
@AnneBeyer

Copy link
Copy Markdown
Contributor Author

maybe inspecting the levels would be a way to achieve this, for instance by including/adapting the test of #32866.

For plot_method=contourf, the plotting is now handled differently, i.e., by adding a separate surface for each class, so the levels are not helping here. Instead, I added two separate checks that there are at least as many distinct colors as classes and that the number of surfaces matches the number of classes.

However, adding the level check part from PR #32867 when plot_method=contour revealed that the bug from #32866 is actually still a problem in this case and will require the adaption of levels proposed there. @leweex95, would you be interested in adapting that once this PR is merged?

"Blues",
["red", "green", "blue"],
(None, 3),
(None, 15),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This test will fail for ("tab10", 15), which I will address in a follow-up PR.

("plasma", 15),
("Blues", 3),
("Blues", 15),
(["red", "green", "blue"], 3),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I had to combine colors and number of classes here because the list version has to match the exact number of classes.

else:
assert_allclose(disp.surface_.colors, colors)

# non-regression test for issue #32866 with `contour` (currently still fails)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This can be fixed by integrating the approach proposed in #32867 once this is merged.

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here is an early pass of feedback with a focus on the meaning of this display and the "n_classes" params when we use this to display the output of the predict method for a regressor.

Comment thread sklearn/inspection/_plot/decision_boundary.py Outdated
Comment thread sklearn/inspection/_plot/decision_boundary.py Outdated
AnneBeyer and others added 4 commits January 9, 2026 11:49
@AnneBeyer
AnneBeyer marked this pull request as ready for review January 9, 2026 16:43
Comment thread sklearn/inspection/_plot/decision_boundary.py Outdated
@AnneBeyer

Copy link
Copy Markdown
Contributor Author

Thank you for all the comments @lucyleeow!

In order to keep this PR on point, I'll only address those related to making predict use multiclass_colors (and some minor fixes you suggested) here.

For the remaining points, which are equally valid, I'll try to summarize them here so we don't forget anything. Those should be addressed in follow-up PRs, though.

Please add if I missed anything!

@AnneBeyer

Copy link
Copy Markdown
Contributor Author

I think I addressed all comments @lucyleeow.
@ogrisel there have been some conceptual changes, could you please check if you still approve?

There are also a few remaining questions where feedback would be welcome.

@StefanieSenger StefanieSenger moved this from In progress to In progress - High Priority in Labs Jan 26, 2026

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I had another look at this PR and this LGTM as it is. It's already a big improvement. +1 for merge and tackling the remaining points discussed in linked issues in follow-up PRs.

@ogrisel

ogrisel commented Jan 30, 2026

Copy link
Copy Markdown
Member

@lucyleeow ok for merge?

@lucyleeow lucyleeow left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for all your work on this, it was quite complicated in the end.

Some nits only and a question but LGTM and fine to merge.

Comment thread sklearn/inspection/_plot/tests/test_boundary_decision_display.py Outdated
Comment thread sklearn/inspection/_plot/decision_boundary.py
Comment thread sklearn/inspection/_plot/decision_boundary.py Outdated
@AnneBeyer

Copy link
Copy Markdown
Contributor Author

Thank you for all the feedback @lucyleeow and @ogrisel! I think now it is good to go.

@lucyleeow

Copy link
Copy Markdown
Member

Thanks for your work and your patience @AnneBeyer ! Merging!

@lucyleeow
lucyleeow merged commit 41f1f34 into scikit-learn:main Feb 2, 2026
40 checks passed
@github-project-automation github-project-automation Bot moved this from In progress - High Priority to Done in Labs Feb 2, 2026
@ogrisel

ogrisel commented Feb 2, 2026

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

DecisionBoundaryDisplay with response_method="predict" has inconsistent handling for the colormap in the multiclass case

5 participants