Skip to content

FIX Array validation for DecisionBoundaryDisplay #25077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 2, 2022
4 changes: 4 additions & 0 deletions doc/whats_new/v1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ Changelog
:pr:`18298` by :user:`Madhura Jayaratne <madhuracj>` and
:user:`Guillaume Lemaitre <glemaitre>`.

- |Fix| :class:`inspection.DecisionBoundaryDisplay` now raises error if input
data is not 2-dimensional.
:pr:`25077` by :user:`Arturo Amor <ArturoAmorQ>`.

:mod:`sklearn.kernel_approximation`
...................................

Expand Down
12 changes: 11 additions & 1 deletion sklearn/inspection/_plot/decision_boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
from ...utils import check_matplotlib_support
from ...utils import _safe_indexing
from ...base import is_regressor
from ...utils.validation import check_is_fitted, _is_arraylike_not_scalar
from ...utils.validation import (
check_is_fitted,
_is_arraylike_not_scalar,
_num_features,
)


def _check_boundary_response_method(estimator, response_method):
Expand Down Expand Up @@ -316,6 +320,12 @@ def from_estimator(
f"Got {plot_method} instead."
)

num_features = _num_features(X)
if num_features != 2:
raise ValueError(
f"n_features must be equal to 2. Got {num_features} instead."
)

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

x0_min, x0_max = x0.min() - eps, x0.max() + eps
Expand Down
10 changes: 10 additions & 0 deletions sklearn/inspection/_plot/tests/test_boundary_decision_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ def fitted_clf():
return LogisticRegression().fit(X, y)


def test_input_data_dimension():
"""Check that we raise an error when `X` does not have exactly 2 features."""
X, y = make_classification(n_samples=10, n_features=4, random_state=0)

clf = LogisticRegression().fit(X, y)
msg = "n_features must be equal to 2. Got 4 instead."
with pytest.raises(ValueError, match=msg):
DecisionBoundaryDisplay.from_estimator(estimator=clf, X=X)


def test_check_boundary_response_method_auto():
"""Check _check_boundary_response_method behavior with 'auto'."""

Expand Down