diff --git a/doc/whats_new/v1.2.rst b/doc/whats_new/v1.2.rst index 70bd9b82d896d..1ce32968581a9 100644 --- a/doc/whats_new/v1.2.rst +++ b/doc/whats_new/v1.2.rst @@ -416,6 +416,10 @@ Changelog :pr:`18298` by :user:`Madhura Jayaratne ` and :user:`Guillaume Lemaitre `. +- |Fix| :class:`inspection.DecisionBoundaryDisplay` now raises error if input + data is not 2-dimensional. + :pr:`25077` by :user:`Arturo Amor `. + :mod:`sklearn.kernel_approximation` ................................... diff --git a/sklearn/inspection/_plot/decision_boundary.py b/sklearn/inspection/_plot/decision_boundary.py index 86836a81f7207..22b4590d9bc3c 100644 --- a/sklearn/inspection/_plot/decision_boundary.py +++ b/sklearn/inspection/_plot/decision_boundary.py @@ -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): @@ -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 diff --git a/sklearn/inspection/_plot/tests/test_boundary_decision_display.py b/sklearn/inspection/_plot/tests/test_boundary_decision_display.py index 8981c9d5a5e83..2d46507ab212c 100644 --- a/sklearn/inspection/_plot/tests/test_boundary_decision_display.py +++ b/sklearn/inspection/_plot/tests/test_boundary_decision_display.py @@ -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'."""