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

Skip to content

BUG: always raise on NaN in OneHotEncoder for object dtype data #12033

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
28 changes: 20 additions & 8 deletions sklearn/preprocessing/_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
import numpy as np
from scipy import sparse

from .. import get_config as _get_config
from ..base import BaseEstimator, TransformerMixin
from ..externals import six
from ..utils import check_array
from ..utils import deprecated
from ..utils.fixes import _argmax
from ..utils.fixes import _argmax, _object_dtype_isnan
from ..utils.validation import check_is_fitted

from .base import _transform_selected
Expand All @@ -37,14 +38,30 @@ class _BaseEncoder(BaseEstimator, TransformerMixin):

"""

def _fit(self, X, handle_unknown='error'):
def _check_X(self, X):
"""
Perform custom check_array:
- convert list of strings to object dtype
- check for missing values for object dtype data (check_array does
not do that)

"""
X_temp = check_array(X, dtype=None)
if not hasattr(X, 'dtype') and np.issubdtype(X_temp.dtype, np.str_):
X = check_array(X, dtype=np.object)
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'):
X = self._check_X(X)

n_samples, n_features = X.shape

if self._categories != 'auto':
Expand Down Expand Up @@ -74,12 +91,7 @@ def _fit(self, X, handle_unknown='error'):
self.categories_.append(cats)

def _transform(self, X, handle_unknown='error'):

X_temp = check_array(X, dtype=None)
if not hasattr(X, 'dtype') and np.issubdtype(X_temp.dtype, np.str_):
X = check_array(X, dtype=np.object)
else:
X = X_temp
X = self._check_X(X)

_, n_features = X.shape
X_int = np.zeros_like(X, dtype=np.int)
Expand Down
37 changes: 37 additions & 0 deletions sklearn/preprocessing/tests/test_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,25 @@ def test_one_hot_encoder_feature_names_unicode():
assert_array_equal([u'n👍me_c❤t1', u'n👍me_dat2'], feature_names)


@pytest.mark.parametrize("X", [np.array([[1, np.nan]]).T,
np.array([['a', np.nan]], dtype=object).T],
ids=['numeric', 'object'])
@pytest.mark.parametrize("handle_unknown", ['error', 'ignore'])
def test_one_hot_encoder_raise_missing(X, handle_unknown):
ohe = OneHotEncoder(categories='auto', handle_unknown=handle_unknown)

with pytest.raises(ValueError, match="Input contains NaN"):
ohe.fit(X)

with pytest.raises(ValueError, match="Input contains NaN"):
ohe.fit_transform(X)

ohe.fit(X[:1, :])

with pytest.raises(ValueError, match="Input contains NaN"):
ohe.transform(X)


@pytest.mark.parametrize("X", [
[['abc', 2, 55], ['def', 1, 55]],
np.array([[10, 2, 55], [20, 1, 55]]),
Expand Down Expand Up @@ -524,6 +543,24 @@ def test_ordinal_encoder_inverse():
assert_raises_regex(ValueError, msg, enc.inverse_transform, X_tr)


@pytest.mark.parametrize("X", [np.array([[1, np.nan]]).T,
np.array([['a', np.nan]], dtype=object).T],
ids=['numeric', 'object'])
def test_ordinal_encoder_raise_missing(X):
ohe = OrdinalEncoder()

with pytest.raises(ValueError, match="Input contains NaN"):
ohe.fit(X)

with pytest.raises(ValueError, match="Input contains NaN"):
ohe.fit_transform(X)

ohe.fit(X[:1, :])

with pytest.raises(ValueError, match="Input contains NaN"):
ohe.transform(X)


def test_encoder_dtypes():
# check that dtypes are preserved when determining categories
enc = OneHotEncoder(categories='auto')
Expand Down