You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Fix and refactor color handling in DecisionBoundaryDisplay #33115
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)?
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.
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.
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?
I played around with the zooming in in combination with the things discussed here (as they are closely related):
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:
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:
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.
@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.
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
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).
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.
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!
Activity
lucyleeow commentedon Jan 22, 2026
Thanks this is a good list!
AnneBeyer commentedon Jan 22, 2026
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).
which internally calls
display.plot()and
Some parameter checks for parameters only relevant for
plothappen infrom_estimatorand then again inplot(specificallyplot_methodandmulticlass_colors).In general, do we prefer to
from_estimatorif some checks fail later inplot)?lucyleeow commentedon Jan 23, 2026
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.
adina8244 commentedon Jan 28, 2026
AnneBeyer commentedon Jan 30, 2026
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 theplotmethod. 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.lucyleeow commentedon Feb 2, 2026
FYI @AnneBeyer I just added links to my extended comments in #33015, next to relevant bullet point, just for posterity.
28 remaining items
lucyleeow commentedon Aug 24, 2026
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).
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 plainplotcall we make whenn_classesis 2? This seems easier than trying to manipulate the predict_proba/df responses for binary cases and it would resolve handling of regression?AnneBeyer commentedon Aug 26, 2026
I played around with the zooming in in combination with the things discussed here (as they are closely related):
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:
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=Nonefor 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:While looking into this, I came across one more special case (unrelated to the zooming in part):
When
class_of_interestis passed, the matplotlibplot_functionis currently also called directly with the extracted class column and passingcmap. 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_interesttoplot) would make it possible to use consistent colors for the separate class plots.mathaziz commentedon Aug 26, 2026
@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
1of the extra dimension, while the modified values go to index0. 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_functioncase, 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.lucyleeow commentedon Aug 26, 2026
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
AnneBeyer commentedon Aug 26, 2026
Indeed. Handling this case separately would also allow us to add this by default (which currently has to be done manually by the user).
AnneBeyer commentedon Sep 2, 2026
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.
lucyleeow commentedon Nov 12, 2026
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)
target_colorsfor 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 perspectivecolorskwarg refer to level, contour line/area, colours, if the user passescmap, this could be affected by the decision function/proba values unless you fix the scale)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.predict_probaanddecision_function:predict_proba, add another dimension of1 - probadecision_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)n_classes=Noneand 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_probaand centre at 0 for decision functions (using something likeCenteredNorm). 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_interestis 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.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_interestin one plot, so I think what @AnneBeyer, you were talking about is plotting several OVR plots (wheren_cassesis 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_colorsto set everything, you get better UX whenclass_of_interestis set, as this changesn_classesto 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)