Skip to content

ENH: Add xlim/ylim parameters to DecisionBoundaryDisplay.from_estimator - #31693

Closed
ShauryaDusht wants to merge 14 commits into
scikit-learn:mainfrom
ShauryaDusht:feature/decision-boundary-xlim-ylim
Closed

ShauryaDusht wants to merge 14 commits into
scikit-learn:mainfrom
ShauryaDusht:feature/decision-boundary-xlim-ylim

Conversation

@ShauryaDusht

@ShauryaDusht ShauryaDusht commented Jul 2, 2025 •

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Towards #27462

What does this implement/fix? Explain your changes.

This PR adds xlim and ylim parameters to DecisionBoundaryDisplay.from_estimator() to allow users to override the automatic data-based plot limits.

  • Added xlim=None and ylim=None parameters to method signature
  • Added validation to ensure limits are tuples with min < max
  • Modified limit calculation to use custom limits when provided
  • Added documentation for both parameters
  • Maintains backward compatibility

@github-actions

github-actions Bot commented Jul 2, 2025 •

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 57dcb14. Link to the linter CI: here

@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.

@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.

Let's also specify in the docstring that eps is ignored if both xlim and ylim given. Could we also move the the new kwargs to underneath eps?

@ShauryaDusht

Copy link
Copy Markdown
Contributor Author

Thanks for the swift review. I will add these changes soon.

@ShauryaDusht

Copy link
Copy Markdown
Contributor Author

Hi @lucyleeow
I've added the tests and the "whats new" entry.
Since DecisionBoundaryDisplay.from_estimator() depends on matplotlib (which took me a while to understand), I've marked it to be skipped in CI tests. However, when matplotlib is available, all tests pass (verified locally).

@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.

Please place your tests in the existing decision boundary test: sklearn/inspection/_plot/tests/test_boundary_decision_display.py.
You will be able to use the test data and fixtures, so you don't have to repeat.

Your test cases are probably too long and more than what is actually needed. If you are using LLMs to help you with tests, please note that they tend to produce verbose code and careful oversight and review by yourself is probably best.

Note also that we don't use test classes. I am happy to take another look after you've carefully reviewed your tests. Thank you

@ShauryaDusht

ShauryaDusht commented Jul 7, 2025 •

Copy link
Copy Markdown
Contributor Author

I’ve moved the tests to sklearn/inspection/_plot/tests/test_boundary_decision_display.py as suggested.

Also removed test classes and refactored the cases to be more generalized

Thank you

@ShauryaDusht ShauryaDusht reopened this Jul 7, 2025
@AnneBeyer

Copy link
Copy Markdown
Member

I tried it with the example code from the docs and noted the following:

image

I guess the problem is that the data is truncated accordingly in from_estimator, but xlim and ylim are not set explicitly for the ax, which is only created in the plot method. I'm not sure on the use cases here, but I guess if I want to zoom in on the data, I also want to zoom in on other details that I plot on top. What do you think @StefanieSenger @lucyleeow?


if ax is None:
_, ax = plt.subplots()

@AnneBeyer AnneBeyer Nov 5, 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.

Suggested change
ax.set_xlim((self.xx0.min(), self.xx0.max()))
ax.set_ylim((self.xx1.min(), self.xx1.max()))

to explicitly set the limits for the axes

@StefanieSenger

StefanieSenger commented Nov 5, 2025 •

Copy link
Copy Markdown
Member

I guess the problem is that the data is truncated accordingly in from_estimator, but xlim and ylim are not set explicitly for the ax, which is only created in the plot method.

@AnneBeyer, I don't have much of the context without spending a few hours (which I can only do next week), but intuitively when setting xlim and ylim I would too expect the whole plot being limited to that subset.

What about in addition to setting xlim and ylim on the plot, we clip the data (for instance using np.clip)?

f"`{name}` must be a tuple of (min, max) with min < max"
)

x0, x1 = _safe_indexing(X, 0, axis=1), _safe_indexing(X, 1, axis=1)

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 think this should be moved into the respective else statements below, as this is not needed if xlim and ylim are given.

@AnneBeyer

AnneBeyer commented Nov 5, 2025 •

Copy link
Copy Markdown
Member

What about in addition to setting xlim and ylim on the plot, we clip the data (for instance using np.clip)?

@StefanieSenger I don't think this is useful here because the decision boundaries will extend beyond the data. In the case above, the scatter plot is only re-using the axes from the display object, so I don't think we have control over the data there, but setting the limits explicitly in the plot function as suggested above seems to do the trick (if I'm not missing anything).

@AnneBeyer

AnneBeyer commented Nov 5, 2025 •

Copy link
Copy Markdown
Member

I was thinking about whether there would be an obvious sanity check on the limits, but apart from the checks already implemented, I don't really see one right now.

I played around with the limits a bit, and as long as you stay in a "boundary area", it works fine:

image

There is a weird edge case, though, that if you zoom into an area within one class, the background color changes to grey, irrespective of data points being present or not, like here:

image

Not sure how likely this is to happen and if we should investigate this further?

@lucyleeow

Copy link
Copy Markdown
Member

I'm not sure on the use cases here, but I guess if I want to zoom in on the data, I also want to zoom in on other details that I plot on top.

I would agree, let's zoom in on everything.

As far as I remember the use case was looking at what the decision boundary looked like, outside of the training data limits (i.e. if predict was used on 'new' data that was more 'extreme' than training data) but @glemaitre may be able to expand on this.

that if you zoom into an area within one class, the background color changes to grey,

Weird! We seem to specify colors so maybe this is a matplotlib behaviour? I think it is worth investigating.

@StefanieSenger

Copy link
Copy Markdown
Member

Interestingly, the problem with the limits not limiting the whole plot, only the boundaries (described here #31693 (comment)), doesn't occur to me (using python: 3.12.2 and matplotlib: 3.9.2)...

@AnneBeyer

Copy link
Copy Markdown
Member

@StefanieSenger I created an environment with python: 3.12.2 and matplotlib: 3.9.2, but when I check out the code from this PR, I still get the same result as above. Only when I apply this fix, the axes are clipped accordingly.

@AnneBeyer

Copy link
Copy Markdown
Member

that if you zoom into an area within one class, the background color changes to grey,

Weird! We seem to specify colors so maybe this is a matplotlib behaviour? I think it is worth investigating.

In the case of "predict", we actually don't specify any colors, which might be related to the problem here (I'll look into that next).

When using "predict_proba", we do select colors, and the problem with the grey background is no longer there, but when plotting on top, this leads to a color inconsistency:
image
Do we want to support plotting on top in the same colors (i.e., should I look into if/how this could be passed through the axis), or is this a too specific use case?

@AnneBeyer

Copy link
Copy Markdown
Member

I think I narrowed down the problem a bit more:
When we limit the plotting area at the point suggested in this PR, we also calculate the responses only for a subset of the data points, which doesn't necessarily include all classes (as in the predict example discussed above). So when the colors are selected in the plot method, different colors may be selected than for the whole plot.
I'll continue looking into ways to solve this.

@AnneBeyer

Copy link
Copy Markdown
Member

On a related note (that I came across again while looking at #32867):

There is a detailed handling of colors using the multiclass_colors parameter, but this is only taken into account when multiclass predict_proba or decision_function are used. The distinction is made based on the output shape, and predict will always have self.response.ndim == 2, but can still be multiclass.

I was wondering if this is a bug or a design decision. Could you give me some feedback on this @lucyleeow?

@lucyleeow

Copy link
Copy Markdown
Member

Do we want to support plotting on top in the same colors (i.e., should I look into if/how this could be passed through the axis), or is this a too specific use case?

AFAICT this is done by doing a separate scatter call, unrelated to our DecisionBoundaryDisplay? I think it would be fine to let the user specify colour themselves here. I also don't think there would be any way to easily store this information in a mpl axes.

@adrinjalali adrinjalali moved this from In progress to In progress - High Priority in Labs Jan 6, 2026
@ShauryaDusht ShauryaDusht closed this by deleting the head repository Jan 7, 2026
@ogrisel

ogrisel commented Jan 8, 2026

Copy link
Copy Markdown
Member

I think it's good if there is an easy way for the user to pass the same color map to DecisionBoundaryDisplay and a subsequent scatter call and the resulting plot has consistent color use for the dots of a given class in the scatter plot and the color used to fill the region of the same class in the decision boundary display plot. The fix implemented in #33015 seems to make that possible (at least for the multiclass setting). We need to check if that's also the case for the binary classification setting but I am pretty sure it is.

@ogrisel

ogrisel commented Jan 8, 2026

Copy link
Copy Markdown
Member

Since @ShauryaDusht does not seem interested to finalize this PR, @AnneBeyer feel free to take over this work (once #33015 has been finalized).

@AnneBeyer

Copy link
Copy Markdown
Member

I think it's good if there is an easy way for the user to pass the same color map to DecisionBoundaryDisplay and a subsequent scatter [...] We need to check if that's also the case for the binary classification setting but I am pretty sure it is.

Currently, the colors are only saved as a class variable in the multiclass case. But we already discussed here that colors should be handled similarly in the binary/multiclass cases, so this will be addressed once we tackle point 4 from that discussion.

@StefanieSenger StefanieSenger moved this from In progress - High Priority to In progress in Labs Jan 26, 2026
@adrinjalali adrinjalali moved this from In progress to In progress - High Priority in Labs Jan 26, 2026
@StefanieSenger StefanieSenger moved this from In progress - High Priority to Todo - High Priority in Labs Feb 2, 2026
@auguste-probabl auguste-probabl moved this from Todo - High Priority to Todo in Labs Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

5 participants