Skip to content

FIX: fixed DecisionBoundaryDisplay to display distinct colours for all classess correctly - #32867

Closed
leweex95 wants to merge 7 commits into
scikit-learn:mainfrom
leweex95:fix-decision-boundary-colors
Closed

leweex95 wants to merge 7 commits into
scikit-learn:mainfrom
leweex95:fix-decision-boundary-colors

Conversation

@leweex95

@leweex95 leweex95 commented Dec 8, 2025 •

Copy link
Copy Markdown
Contributor

This PR aims to fix a bug where DecisionBoundaryDisplay.from_estimator only displayed up to 7 distinct colors for classifiers with more than 7 classes when using plot_method="contourf" or plot_method="contour"

Fixes #32866

@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Dec 8, 2025

@ThexXTURBOXx ThexXTURBOXx left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This fixes the linked issue - no problems remain. Thank you very much!

@StefanieSenger

Copy link
Copy Markdown
Member

Thanks for the PR, @leweex95!

@AnneBeyer, since you are concerned with Displays, would you be interested in triaging the corresponding issue and reviewing this PR?

@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 am assessing the impact of this PR visually using the following code:

Code

Details
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.inspection import DecisionBoundaryDisplay


data = np.array(
    [
        [-1, -1],
        [-2, -1],
        [1, 1],
        [2, 1],
        [2, 2],
        [3, 2],
        [3, 3],
        [4, 3],
        [4, 4],
        [5, 4],
        [5, 5],
    ]
)
# target = np.asarray([str(i) for i in range(11)])
target = np.arange(11)
clf = LogisticRegression().fit(data, target)

cmap = "gist_rainbow"
_, axes = plt.subplots(nrows=3, ncols=3, figsize=(12, 12), constrained_layout=True)
for plot_method_idx, plot_method in enumerate(["contourf", "contour", "pcolormesh"]):
    for response_method_idx, response_method in enumerate(
        ["predict_proba", "decision_function", "predict"]
    ):
        ax = axes[plot_method_idx, response_method_idx]
        display = DecisionBoundaryDisplay.from_estimator(
            clf,
            data,
            multiclass_colors=cmap,
            response_method=response_method,
            plot_method=plot_method,
            ax=ax,
            alpha=0.5,
        )
        ax.scatter(
            data[:, 0],
            data[:, 1],
            c=target.astype(int),
            edgecolors="black",
            cmap=cmap,
        )
        if plot_method != "pcolormesh":
            if isinstance(display.surface_, list):
                levels = len(display.surface_[0].levels)
            else:
                levels = len(display.surface_.levels)
            ax.set_title(f"plot_method={plot_method}\nresponse_method={response_method}\nlevels={levels}")
        else:
            ax.set_title(f"plot_method={plot_method}\nresponse_method={response_method}")

Results

main

image

this branch

image

Analysis

So I confirm that this fixes two bugs:

  • one for contour plot method in conjunction with the predict response method;
  • one for contourf plot method in conjunction with the predict_proba and decision_function response methods.

I think the changelog should be adjusted to better reflect this.

See also some further suggestions to improve the tests.

[5, 5],
]
)
y = np.arange(11)

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.

Could you please parametrize this test to make sure that it still works as expected when the dtype of y is object with str instances as class labels?

I think the code in this PR should work since from_estimator always uses a LabelEncoder but better cover this case in this test.

clf = LogisticRegression().fit(pts, y)

disp = DecisionBoundaryDisplay.from_estimator(
clf, pts, response_method="predict", plot_method=plot_method

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.

Please extend this test for predict_proba and decision_function as well, at least for the "contour".

@ogrisel

ogrisel commented Dec 9, 2025

Copy link
Copy Markdown
Member

Maybe @lucyleeow would also be interested in taking a look at this PR given her past involvement with this class.

@AnneBeyer AnneBeyer 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 the PR, @leweex95!

In general, it looks good to me, I just added a few comments below.

)
else:
levels = unique_levels
kwargs.setdefault("levels", levels)

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'm not sure if this is too defensive (I'm not too familiar with the use of levels), but since the user can also pass kwargs (like levels) directly to from_estimator, maybe this should only be computed and added if levels is not given?

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.

Good point. And the test should be updated accordingly.

# Plot only argmax map for contour
class_map = self.response.argmax(axis=2)
# Ensure levels match the number of classes for distinct colors
kwargs.setdefault("levels", np.unique(class_map))

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.

Same as above, only set if not given?

)

# Check that the surface has levels for all classes
if plot_method in ("contourf", "contour"):

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.

plot_method can only be either "contourf" or "contour" (as defined by parametrize), so I don't think this check is needed.

@ogrisel ogrisel Dec 9, 2025 •

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 can also be set to "pcolormesh" (see the code snippet I posted above) but maybe this is not consistently documented?

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.

In general, yes, but this test is (at least currently) only testing "contourf" or "contour". So if the test is extended, it should stay, otherwise, it is not necessary here.

@ogrisel ogrisel Dec 9, 2025 •

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.

Ah indeed, sorry for the noisy comment. Since this test is about testing the actual number of levels which is undefined for "pcolormesh", let's remove this condition.

@leweex95

Copy link
Copy Markdown
Contributor Author

Thanks for your comments, @AnneBeyer and @ogrisel , pushed the fixes!

@lucyleeow

lucyleeow commented Dec 15, 2025 •

Copy link
Copy Markdown
Member

Thanks for your PR @leweex95, I think it would be best if #32872 was addressed first (see #32872 (comment) for details) - that fix would probably remove the binary (and predict)/multiclass logic we have currently for color handling, and also it would probably pass n_classes information, so we can avoid any expensive np.unique calls.

@AnneBeyer is going to work on #32872. You're welcome to pick this up after #32872 is fixed (there may be some significant merge conflicts though, so you may find it easier to start a new PR).

Edit: Note for the new PR I think we should add a TODO to delete this extra logic once our min matplotlib version is >=3.11 since it has been fixed in matplotlib/matplotlib#27576

@AnneBeyer

Copy link
Copy Markdown
Member

One other related thing I noticed while working on #33015:

The plots for plot_method=contour all look the same in the example above. Is this desired, or should they also include the class specific contour lines like this (which I created accidentally by using if here instead of elif):
image
(This would require some more adaption of the corresponding tests, as the internal surface logic is handled differently in this case as well, so I didn't touch this in my PR.)

@ogrisel

ogrisel commented Jan 8, 2026

Copy link
Copy Markdown
Member

The plots for plot_method=contour all look the same in the example above. Is this desired, or should they also include the class specific contour lines like this (which I created accidentally by using if here instead of elif)

Those contour plots look interesting, but I assume that to make them correct, one would need to ensure that the number of levels is high enough to completely separate all pairs of classes. Maybe we could first finalize #33015 and the fix for the number of levels to ensure that decision boundaries are properly drawn for all pairs of classes and then explore this idea gain in a follow-up PR to keep the review process lightweight?

@lucyleeow

Copy link
Copy Markdown
Member

Note I opened a separate issue for the levels + contour issues noted above: #33108

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

Copy link
Copy Markdown
Member

Hi @leweex95, thank you for your patience! Now that #32872 is closed, would you like to take this up again?

One related thing I noticed:
The contourf plot for predict here also uses a duplicate color, even though n_classes is only 7 and len(levels)=9, but the levels don't correspond to the classes. Could you take a look at that case as well?

@auguste-probabl auguste-probabl moved this from In progress - High Priority to In progress in Labs Feb 9, 2026
@AnneBeyer

Copy link
Copy Markdown
Member

Given that there was no response from @leweex95, I will continue this (in a new PR to avoid merge conflicts with #33015).

@virchan virchan 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 the PR, @leweex95, and for helping improve scikit-learn.

Closing as superseded by #33300.

@virchan virchan added the Superseded PR has been replace by a newer PR label Feb 18, 2026
@virchan virchan closed this Feb 18, 2026
@github-project-automation github-project-automation Bot moved this from In progress to Done in Labs Feb 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:inspection Superseded PR has been replace by a newer PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DecisionBoundaryDisplay.from_estimator only displays up to 7 distinct colours

7 participants