Repository navigation
Description
Describe the bug
This issue was discovered while reviewing #32867, but since it's not directly related, let's open a dedicated issue to avoid derailing the original discussion.
As can be seen in the plots below, passing multiclass_colors="gist_rainbow" has no impact when response_method="predict" but I don't see any reason why it wouldn't be the case.
I think we should refactor the code to have consistent colormap configuration for all the response methods (in particular for the multiclass case).
Steps/Code to Reproduce
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],
]
)
target = np.arange(data.shape[0])
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,
)
ax.set_title(f"plot_method={plot_method}\nresponse_method={response_method}")Expected Results
I would expect the colored areas of the right most columns to also use the "gist_rainbow" colormap as the other columns.
Actual Results
The right most column seems to ignore the multiclass_colors argument and falls back to the default "viridis" colormap which is not very well suited for multiclass problems with a large number of classes.
Versions
System:
python: 3.13.7 | packaged by conda-forge | (main, Sep 3 2025, 14:24:46) [Clang 19.1.7 ]
executable: /Users/ogrisel/miniforge3/envs/dev/bin/python3.13
machine: macOS-15.6.1-arm64-arm-64bit-Mach-O
Python dependencies:
sklearn: 1.9.dev0
pip: 25.2
setuptools: 80.9.0
numpy: 2.3.3
scipy: 1.16.3
Cython: 3.1.4
pandas: 3.0.0.dev0+2566.g2bb3fef887
matplotlib: 3.10.6
joblib: 1.5.2
threadpoolctl: 3.6.0
Built with OpenMP: True
threadpoolctl info:
user_api: blas
internal_api: openblas
num_threads: 10
prefix: libopenblas
filepath: /Users/ogrisel/miniforge3/envs/dev/lib/libopenblas.0.dylib
version: 0.3.30
threading_layer: openmp
architecture: VORTEX
user_api: openmp
internal_api: openmp
num_threads: 10
prefix: libomp
filepath: /Users/ogrisel/miniforge3/envs/dev/lib/libomp.dylib
version: None
Activity
ogrisel commentedon Dec 9, 2025
cc @lucyleeow @AnneBeyer
[-]`DecisionBoundaryDisplay` with `response_method="predict"` has consistent handling for the colormap in the multiclass case[/-][+]`DecisionBoundaryDisplay` with `response_method="predict"` has inconsistent handling for the colormap in the multiclass case[/+]AnneBeyer commentedon Dec 9, 2025
Yes, I also came across this here, and it should indeed be fixed separately.
@lucyleeow let me know if you want to address it or if I should give it a try.
PR-HARIHARAN commentedon Dec 10, 2025
ogrisel commentedon Dec 10, 2025
AnneBeyer commentedon Dec 12, 2025
Starting to look into this, I have a general question:
What is the reason for handling colors by different parameters in the binary (currently including multiclass
predict) and multiclass cases?I find it a bit confusing that one has to use the 'mpl' parameters in the binary case, but
multiclass_colorsotherwise, wherecmap/colorswill be ignored/overwritten if given.I think it could be changed to use one parameter (
class_colors?) to set the colors/color palette in all case, and make the distinction internally. I'm just not sure if I'm overlooking anything here? @lucyleeowPushkar881 commentedon Dec 14, 2025
It seems that I can replicate that too. While using
response_method="predict", the argumentmulticlass_colorsdoes not appear to make any difference at all, unlike other response methods.As this issue is already allocated, I just wanted to clarify if this is supposed to work in this way or it is expected behavior. Happy to assist with testing or reviewing if required.
lucyleeow commentedon Dec 15, 2025
Responding to some of the comments from #31693, as well here to have the discussion in a single place.
With regard to the 2 issues below:
(from #31693 (comment))
(From above)
I can't think of a reason why multiclass
predictshould be handled differently. I think it was an oversight as #29797 was originally about adding support for plotting of multiclassdecision_functionandpredict_probas(predictwas already supported), we when themulticlass_colorsparameter was added later in the PR, we just didn't think aboutpredict.Again, just because we were thinking about multiclass. I would be happy to deprecate
multiclass_colorsforclass_colors(or maybelabel_colors?). Current input options:I think the default
multiclass_colorsvalue currently (tab10) would be reasonable for binary case, as the first 2 colors are blue and orange. I do think we should advise against use of a string mpl colormap for use in the binary case in the docs, but probably fine if allow it.I see several issues:
predictignoresmulticlass_colorsDecisionBoundaryDisplay.from_estimatoronly displays up to 7 distinct colours #32866 - mpl bug when number of classes >7, that is propagatedmulticlass_colors->class_colors(or maybelabel_colors?)It seems that items 1,2,3 all would benefit from knowing the full number of classes, which we can obtain from the fitted
estimator.classes_(and would avoid expensive calls tonp.unique, which is done in #32867). There are 2 ways we could do this, pass number of classes (i.e.n_responses, which we currently use inplot) toplot(probably via class instantiation infrom_estimator), or move the color and level logic (currently handled byplot) tofrom_estimator.I think the former (passing
n_responses/n_classes) to plot would be preferable (it certainly would reduce the git diff) but you may have a better feel @AnneBeyer when you implement this.Also I think issue 1 should be addressed first - it seems a more important bug to fix, and the restructure (i.e. passing of
n_classeswould be useful for subsequent issues).Pushkar881 commentedon Dec 15, 2025
lucyleeow commentedon Dec 15, 2025
AnneBeyer commentedon Dec 15, 2025
Thank you for your thoughts on this, @lucyleeow!
I agree, I'll start with a fix for the current issue, and my plan is also to add
n_classes(or mayben_labels?) to the class constructor as in your first suggestion.Then we can look into the rest separately.
lucyleeow commentedon Dec 15, 2025
I think slight preference for
n_classesbecause we call itestimator.classes_AnneBeyer commentedon Dec 16, 2025
One problem I ran into with this approach: DecisionBoundaryDisplay also supports regressors, which don't have
n_classes, so in this case we do need to fall back to the unique values ofy, right?AnneBeyer commentedon Dec 16, 2025
Two other related side issues:
class_of_interestshould also be considered with multiclasspredict, right?Currently, it can only be set with
predict_probaordecision_functionand ignored otherwise, but I think it also makes sense in the other multiclass case. I would address this in a separate PR, in order to keep things as simple as possible.If
class_of_interestis set (withpredict_probaordecision_function), the colormap is currently also ignored, (as this converts it into a binary problem), which should be fixed once we address Issue 4 in the comment abovelucyleeow commentedon Dec 17, 2025
Indeed. It also supports clustering and outlier detection estimators which also do not have
classesattribute.+1
lucyleeow commentedon Dec 23, 2025
Thinking about this more, I realise we will not be able to solve the problem of not all 'prediction' values being represented when we zoom in by setting x/ylim (#31693 (comment)).
For regression estimators this problem is definitely not solve-able - I do question the usefulness of
DecisionBoundaryDisplayon many regression problems though. Not sure of the best approach here, cc @glemaitre in case he has some advice.For clustering estimators, I think
labels_is standard - inClusterMixinyou have:scikit-learn/sklearn/base.py
Lines 718 to 719 in b0ba8b0
(and of course for outlier detection, you can only have 2 states, outlier or inlier)