Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Follow force_all_finite for object dtype in check_array #13254

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
5 changes: 0 additions & 5 deletions sklearn/preprocessing/_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ def _check_X(self, X):
else:
X = X_temp

if X.dtype == np.dtype('object'):
if not _get_config()['assume_finite']:
if _object_dtype_isnan(X).any():
raise ValueError("Input contains NaN")

return X

def _fit(self, X, handle_unknown='error'):
Expand Down
13 changes: 13 additions & 0 deletions sklearn/utils/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,19 @@ def test_check_array_force_all_finiteinvalid(value, force_all_finite,
accept_sparse=True)


def test_check_array_force_all_finite_object():
X = np.array([['a', 'b', np.nan]], dtype=object).T

X_checked = check_array(X, dtype=None, force_all_finite='allow-nan')
assert X is X_checked

X_checked = check_array(X, dtype=None, force_all_finite=False)
assert X is X_checked

with pytest.raises(ValueError, match='Input contains NaN'):
check_array(X, dtype=None, force_all_finite=True)


@ignore_warnings
def test_check_array():
# accept_sparse == None
Expand Down
4 changes: 4 additions & 0 deletions sklearn/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from numpy.core.numeric import ComplexWarning

from .fixes import _object_dtype_isnan
from .. import get_config as _get_config
from ..exceptions import NonBLASDotWarning
from ..exceptions import NotFittedError
Expand Down Expand Up @@ -53,6 +54,9 @@ def _assert_all_finite(X, allow_nan=False):
not allow_nan and not np.isfinite(X).all()):
type_err = 'infinity' if allow_nan else 'NaN, infinity'
raise ValueError(msg_err.format(type_err, X.dtype))
elif X.dtype == np.dtype('object') and not allow_nan:
if _object_dtype_isnan(X).any():
raise ValueError("Input contains NaN")


def assert_all_finite(X, allow_nan=False):
Expand Down