Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 72b35a4

Browse filesBrowse files
MNT Improve error check_array error message when estimator is None (#30485)
Co-authored-by: Loïc Estève <loic.esteve@ymail.com>
1 parent 485d39c commit 72b35a4
Copy full SHA for 72b35a4

File tree

2 files changed

+23
-2
lines changed
Filter options

2 files changed

+23
-2
lines changed

‎sklearn/utils/tests/test_validation.py

Copy file name to clipboardExpand all lines: sklearn/utils/tests/test_validation.py
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,3 +2358,24 @@ def test_force_all_finite_rename_warning():
23582358

23592359
with pytest.warns(FutureWarning, match=msg):
23602360
as_float_array(X, force_all_finite=True)
2361+
2362+
2363+
@pytest.mark.parametrize(
2364+
["X", "estimator", "expected_error_message"],
2365+
[
2366+
(
2367+
np.array([[[1, 2], [3, 4]], [[1, 2], [3, 4]]]),
2368+
RandomForestRegressor(),
2369+
"Found array with dim 3, while dim <= 2 is required by "
2370+
"RandomForestRegressor.",
2371+
),
2372+
(
2373+
np.array([[[1, 2], [3, 4]], [[1, 2], [3, 4]]]),
2374+
None,
2375+
"Found array with dim 3, while dim <= 2 is required.",
2376+
),
2377+
],
2378+
)
2379+
def test_check_array_allow_nd_errors(X, estimator, expected_error_message):
2380+
with pytest.raises(ValueError, match=expected_error_message):
2381+
check_array(X, estimator=estimator)

‎sklearn/utils/validation.py

Copy file name to clipboardExpand all lines: sklearn/utils/validation.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,8 +1097,8 @@ def is_sparse(dtype):
10971097
)
10981098
if not allow_nd and array.ndim >= 3:
10991099
raise ValueError(
1100-
"Found array with dim %d. %s expected <= 2."
1101-
% (array.ndim, estimator_name)
1100+
f"Found array with dim {array.ndim},"
1101+
f" while dim <= 2 is required{context}."
11021102
)
11031103

11041104
if ensure_all_finite:

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.