Skip to content

[MRG+1] Rework warning in check_array when silent convert string to float #11577

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 8 additions & 31 deletions sklearn/utils/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,40 +291,17 @@ def test_check_array():
assert_true(isinstance(result, np.ndarray))

# deprecation warning if string-like array with dtype="numeric"
X_str = [['a', 'b'], ['c', 'd']]
assert_warns_message(
FutureWarning,
"arrays of strings will be interpreted as decimal numbers if "
"parameter 'dtype' is 'numeric'. It is recommended that you convert "
"the array to type np.float64 before passing it to check_array.",
check_array, X_str, "numeric")
assert_warns_message(
FutureWarning,
"arrays of strings will be interpreted as decimal numbers if "
"parameter 'dtype' is 'numeric'. It is recommended that you convert "
"the array to type np.float64 before passing it to check_array.",
check_array, np.array(X_str, dtype='U'), "numeric")
assert_warns_message(
FutureWarning,
"arrays of strings will be interpreted as decimal numbers if "
"parameter 'dtype' is 'numeric'. It is recommended that you convert "
"the array to type np.float64 before passing it to check_array.",
check_array, np.array(X_str, dtype='S'), "numeric")
expected_warn_regex = r"converted to decimal numbers if dtype='numeric'"
X_str = [['11', '12'], ['13', 'xx']]
for X in [X_str, np.array(X_str, dtype='U'), np.array(X_str, dtype='S')]:
with pytest.warns(FutureWarning, match=expected_warn_regex):
check_array(X, dtype="numeric")

# deprecation warning if byte-like array with dtype="numeric"
X_bytes = [[b'a', b'b'], [b'c', b'd']]
assert_warns_message(
FutureWarning,
"arrays of strings will be interpreted as decimal numbers if "
"parameter 'dtype' is 'numeric'. It is recommended that you convert "
"the array to type np.float64 before passing it to check_array.",
check_array, X_bytes, "numeric")
assert_warns_message(
FutureWarning,
"arrays of strings will be interpreted as decimal numbers if "
"parameter 'dtype' is 'numeric'. It is recommended that you convert "
"the array to type np.float64 before passing it to check_array.",
check_array, np.array(X_bytes, dtype='V1'), "numeric")
for X in [X_bytes, np.array(X_bytes, dtype='V1')]:
with pytest.warns(FutureWarning, match=expected_warn_regex):
check_array(X, dtype="numeric")


def test_check_array_pandas_dtype_object_conversion():
Expand Down
10 changes: 6 additions & 4 deletions sklearn/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,12 @@ def check_array(array, accept_sparse=False, accept_large_sparse=True,
# in the future np.flexible dtypes will be handled like object dtypes
if dtype_numeric and np.issubdtype(array.dtype, np.flexible):
warnings.warn(
"Beginning in version 0.22, arrays of strings will be "
"interpreted as decimal numbers if parameter 'dtype' is "
"'numeric'. It is recommended that you convert the array to "
"type np.float64 before passing it to check_array.",
"Beginning in version 0.22, arrays of bytes/strings will be "
"converted to decimal numbers if dtype='numeric'. "
"It is recommended that you convert the array to "
"a float dtype before using it in scikit-learn, "
"for example by using "
"your_array = your_array.astype(np.float64).",
FutureWarning)

# make sure we actually converted to numeric:
Expand Down