Skip to content
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

[MRG] FIX: raise error with inconsistent dtype X and missing_values #11391

Merged
merged 7 commits into from Jul 16, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion sklearn/impute.py
Expand Up @@ -40,6 +40,15 @@
]


def _check_inputs_dtype(X, missing_values):
if (X.dtype.kind in ("f", "i", "u") and
not isinstance(missing_values, numbers.Real)):
raise ValueError("'X' and 'missing_values' types are expected to be"
" both numerical. Got X.dtype={} and "
" type(missing_values)={}."
.format(X.dtype, type(missing_values)))


def _get_mask(X, value_to_mask):
"""Compute the boolean mask X == missing_values."""
if value_to_mask is np.nan:
Expand All @@ -51,7 +60,6 @@ def _get_mask(X, value_to_mask):
else:
# np.isnan does not work on object dtypes.
return _object_dtype_isnan(X)

else:
# X == value_to_mask with object dytpes does not always perform
# element-wise for old versions of numpy
Expand Down Expand Up @@ -183,6 +191,7 @@ def _validate_input(self, X):
else:
raise ve

_check_inputs_dtype(X, self.missing_values)
if X.dtype.kind not in ("i", "u", "f", "O"):
raise ValueError("SimpleImputer does not support data with dtype "
"{0}. Please provide either a numeric array (with"
Expand Down Expand Up @@ -788,6 +797,7 @@ def _initial_imputation(self, X):

X = check_array(X, dtype=FLOAT_DTYPES, order="F",
force_all_finite=force_all_finite)
_check_inputs_dtype(X, self.missing_values)

mask_missing_values = _get_mask(X, self.missing_values)
if self.initial_imputer_ is None:
Expand Down
22 changes: 22 additions & 0 deletions sklearn/tests/test_impute.py
Expand Up @@ -705,3 +705,25 @@ def test_chained_imputer_additive_matrix():
random_state=rng).fit(X_train)
X_test_est = imputer.transform(X_test)
assert_allclose(X_test_filled, X_test_est, atol=0.01)


@pytest.mark.parametrize("imputer_constructor",
[SimpleImputer, ChainedImputer])
@pytest.mark.parametrize(
"imputer_missing_values, missing_value, err_msg",
[("NaN", np.nan, "Input contains NaN"),
("-1", -1, "types are expected to be both numerical.")])
def test_inconsistent_dtype_X_missing_values(imputer_constructor,
imputer_missing_values,
missing_value,
err_msg):
# regression test for issue #11390. Comparison between incoherent dtype
# for X and missing_values was not raising a proper error.
rng = np.random.RandomState(42)
X = rng.randn(10, 10)
X[0, 0] = missing_value

imputer = imputer_constructor(missing_values=imputer_missing_values)

with pytest.raises(ValueError, match=err_msg):
imputer.fit_transform(X)