Skip to content

Fix and refactor color handling in DecisionBoundaryDisplay #33115

Open

Description

@AnneBeyer

This is to keep track and address several things we noticed in #33015

I will address them in follow-up PRs.

Please add, if I missed anything @lucyleeow @ogrisel

Activity

  1. added
    Needs Triage
    on Jan 20, 2026
  2. removed
    Needs Triage
    on Jan 20, 2026
  3. lucyleeow commented on Jan 22, 2026

    @lucyleeow
    Member

    Thanks this is a good list!

  4. AnneBeyer commented on Jan 22, 2026

    @AnneBeyer
    MemberAuthor

    A general question (but related to point 3 above):

    There are two paths to construct a display (we recommend the first, but the second is also possible and used in a few examples).

    display = DecisionBoundaryDisplay.from_estimator(...)
    

    which internally calls display.plot()
    and

    display = DecisionBoundaryDisplay(...)
    display.plot()
    

    Some parameter checks for parameters only relevant for plot happen in from_estimator and then again in plot (specifically plot_method and multiclass_colors).

    In general, do we prefer to

    • fail fast and have duplicate checks in both or
    • avoid redundancies and only check once (with the cost of e.g., unnecessary computation of responses in from_estimator if some checks fail later in plot)?
  5. lucyleeow commented on Jan 23, 2026

    @lucyleeow
    Member

    Good question. I recall we had this discussion previously with Guillaume and Jeremie but I can't find it.

    I would said the latter - avoid redundancies and only check once. Since this is a display class, efficiency is not as important. I think this was what we settled on previously.

  6. moved this from Todo to Todo - High Priority in Labson Jan 26, 2026
  7. adina8244 commented on Jan 28, 2026

    @adina8244
  8. AnneBeyer commented on Jan 30, 2026

    @AnneBeyer
    MemberAuthor

    Hi @adina8244, thank you for your interest and the valid suggestion. Since I am in the process of adding more functionality to the DecisionBoundaryDisplays, I would prefer to keep the code as simple as possible. While I like your approach, I opened an issue on the general use of the plot method. If anyone is against removing it, I would be in favor of following your approach, but let's wait for the discussion in #33159 to come to a conclusion.

  9. lucyleeow commented on Feb 2, 2026

    @lucyleeow
    Member

    FYI @AnneBeyer I just added links to my extended comments in #33015, next to relevant bullet point, just for posterity.

  10. moved this from Todo - High Priority to In progress - High Priority in Labson Feb 2, 2026
  11. moved this from In progress - High Priority to In progress in Labson Feb 9, 2026
  12. 28 remaining items

  13. moved this from Todo to In progress in Labson Aug 20, 2026
  14. moved this from In progress to PR waiting for reviews in Labson Aug 20, 2026
  15. lucyleeow commented on Aug 24, 2026

    @lucyleeow
    Member

    The shading in the predict_proba and decision_function cases (and the different regions for regression) would be re-computed based on the values in the zoomed in region nevertheless in the current approach

    My first thought here is that the colours matching may not be a problem as it is for discrete classes - I imagine there is a legend for the colour gradients and it may even be a feature as one zooming in may wish to discriminate between smaller value range, so the recomputed gradient may actually be desired ( not 100% sure though).

    The question I ask myself is, why do we make the distinction between the two cases?

    It seems that the distinction is due to handling regression and unthresholded binary classification responses.
    Would it be possible to somehow use target_colors_ (modifying it if required) in the plain plot call we make when n_classes is 2? This seems easier than trying to manipulate the predict_proba/df responses for binary cases and it would resolve handling of regression?

  16. moved this from PR waiting for reviews to In progress in Labson Aug 24, 2026
  17. AnneBeyer commented on Aug 26, 2026

    @AnneBeyer
    MemberAuthor

    I played around with the zooming in in combination with the things discussed here (as they are closely related):

    1. Binary classification
      I think for binary classification, we should change the plotting to derive the corresponding values for the second class from the first and plot the classes separately like in the multiclass case. Because when zooming in and re-computing the colors, the decision boundary will seem to change like this when using a continuous colormap:
    Image
    1. Regression
      In this case, the legend is necessary, because the colors change meaning when zooming in, whereas a more fine-grained gradient can be achieved with increasing the levels. I think I would prefer the zoomed in version to only use more shades within the color-region of that area, but I'm fine with this approach (which can simply be achieved by defining n_classes=None for regression and then keeping the plotting as it is for this condition). If we keep the current handling for regression, zooming in will look like this:
    Image
    1. Class of interest
      While looking into this, I came across one more special case (unrelated to the zooming in part):
      When class_of_interest is passed, the matplotlibplot_function is currently also called directly with the extracted class column and passing cmap. Here, the "fading to white" is done by explicitly choosing a cmap that is fading to white ("Blues").
      Treating this as an instance of multiclass (and passing the whole response and class_of_interest to plot) would make it possible to use consistent colors for the separate class plots.
  18. mathaziz commented on Aug 26, 2026

    @mathaziz
    Contributor

    @AnneBeyer Nice development on the zooming part. I agree that treating the binary class as an instance of multi-class makes the more sense.

    If you go down this route and add a new dimension to the response matrix in the binary case, just be aware that the response matrix concerns the second class. So when you add a dimension, the current response matrix goes to index 1 of the extra dimension, while the modified values go to index 0. Maybe you already know this, but this has puzzled me at first because the colors (red and pink) got inverted in my plots (#33115 (comment)).

    Also, in the decision_function case, I confirm that the new dimension gets the response matrix multiplied by -1. You can take a look at the description of _process_decision_function used in _get_response_values.

  19. lucyleeow commented on Aug 26, 2026

    @lucyleeow
    Member

    In this case, the legend is necessary, because the colors change meaning when zooming in,

    Wouldn't a legend always be necessary for regression, since is not decision boundaries we are showing but regression response values which could be any value scale?
    Again this brings to light how different regression plotting is in this display class

  20. AnneBeyer commented on Aug 26, 2026

    @AnneBeyer
    MemberAuthor

    Wouldn't a legend always be necessary for regression, since is not decision boundaries we are showing but regression response values which could be any value scale?

    Indeed. Handling this case separately would also allow us to add this by default (which currently has to be done manually by the user).

  21. AnneBeyer commented on Sep 2, 2026

    @AnneBeyer
    MemberAuthor

    I played around with this some more and have a working version now. There are still some things left to discuss, but I think this is easier with the corresponding code, so I'll open a draft PR for this.

  22. moved this from In progress to Todo in Labson Sep 21, 2026
  23. moved this from Todo to Issue in progress in Labson Sep 21, 2026
  24. moved this from Issue in progress to Todo in Labson Oct 27, 2026
  25. lucyleeow commented on Nov 12, 2026

    @lucyleeow
    Member

    Looking deeper into this, I can see it's quite a complicated issue. Hopefully we can find some reasonable solution.

    I think these are the issues we need to consider in our implementation:

    (I will use the term non-multiclass to encompass binary classification, regression and outlier detection, since technically the 'binary' fork handles all these)

    • Currently, colour is controlled via target_colors for multiclass problems and via kwargs passed directly to matplotlib for non-multiclass problems - it would be nice if they were both handled the same way from a UX/API perspective
    • We need to be able to keep track of consistent class colours in order to plot a scatter plot over top
      • with the current implementation we can't always infer colours correctly for binary problems (colors kwarg refer to level, contour line/area, colours, if the user passes cmap, this could be affected by the decision function/proba values unless you fix the scale)
    • How to handle colours (i.e. the norm) when zooming/extrapolating (i.e. changing the grid min and max)
      • it would be reasonable for decision boundaries (0 for decision function and 0.5 for proba) to have consistent colouring

    If we do want to be able to add a scatter plot and keep class colours consistent, we can't allow binary problems to be handled via kwargs to plot_function, and I think this forces our hand to the solution you both came up with above.

    Potential Implementation

    Handle all colours via target_colors - this fixes the UX problem and allows us to keep track of class colours for binary (and outlier) problems.

    • Add extra dimension for 2D predict_proba and decision_function:
      • For binary predict_proba, add another dimension of 1 - proba
      • For binary, outlier detection decision_function, add another dimension of -df (I think this works for outlier detection as well as skl outlier estimators conform to 0 being the threshold)
    • Regression - handle via n_classes=None and pass argument directly to plot's kwargs (we can also consider dropping support for regression but this may be controversial and if it's not too much trouble we can just keep)

    For binary problems:
    Fix the norm to 0-1 for predict_proba and centre at 0 for decision functions (using something like CenteredNorm). It keeps the decision boundary the same colour in both cases. Since df min max range can vary between estimators (and is often neg inf to positive infinity) we can't really fix it to a range, but as long as the decision boundary is the same colour, I think it is fine.
    It does mean that if we do focus on an area with a small range e.g., for predict proba 0.1-0.3, we won't see a great deal of variation in colour, but I think this is outweighed by having the decision boundary the same colour.

    For multiclass problems, I think fixing the norm is less of a problem and it would be a lot more complicated to determine the best range (I think the boundary between 2 classes for would be when the df/proba is the same for the 2 classes?)


    I'm putting this down at the bottom because the problem is complex enough.

    When class_of_interest is set, for multiclass problems we an 'artificial' n_classes=2' case - there are may be several classes, but for the plot, which is effectively one vs rest, you only need 2 colours.

    When class_of_interest is passed, the matplotlibplot_function is currently also called directly with the extracted class column and passing cmap. Here, the "fading to white" is done by explicitly choosing a cmap that is fading to white ("Blues").

    In the example plot above, the OVR plot is just blue for the class of interest, fading to white for the 'rest' of the classes. I think you would only want to plot points from class_of_interest in one plot, so I think what @AnneBeyer, you were talking about is plotting several OVR plots (where n_casses is 2) side by side, and keeping the class colours consistent? I think here the default can be that we just plot the scatter points using the colour from the gradient colour map (blue here). Allowing scatter colours that are different from the contour plots gradient may just been too complicated...

    (edit: Additionally, with using target_colors to set everything, you get better UX when class_of_interest is set, as this changes n_classes to 2, meaning the problem is multiclass but you set colours as if for binary)


    I may very well have gotten some thing wrong or mixed up here, thoughts very welcome!

    cc @AnneBeyer @mathaziz (and @ogrisel @jeremiedbb in case you want to weigh in)

  26. moved this from Todo to PR in progress in Labson Nov 12, 2026
  27. moved this from PR in progress to Issue in progress in Labson Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions