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

Skip to content
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
4 changes: 4 additions & 0 deletions doc/whats_new/v1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,10 @@ Changelog
raise an exception if the user provided categories contain duplicates.
:pr:`27328` by :user:`Xuefeng Xu <xuefeng-xu>`.

- |Fix| Raise a `NotFittedError` in :class:`preprocessing.OrdinalEncoder` when calling
`transform` without calling `fit` since `categories` always requires to be checked.
:pr:`27821` by :user:`Guillaume Lemaitre <glemaitre>`.

:mod:`sklearn.tree`
...................

Expand Down
1 change: 1 addition & 0 deletions sklearn/preprocessing/_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -1600,6 +1600,7 @@ def transform(self, X):
X_out : ndarray of shape (n_samples, n_features)
Transformed input.
"""
check_is_fitted(self, "categories_")
X_int, X_mask = self._transform(
X,
handle_unknown=self.handle_unknown,
Expand Down
27 changes: 15 additions & 12 deletions sklearn/preprocessing/tests/test_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,6 @@ def test_one_hot_encoder_handle_unknown(handle_unknown):
assert_allclose(X2, X2_passed)


def test_one_hot_encoder_not_fitted():
X = np.array([["a"], ["b"]])
enc = OneHotEncoder(categories=["a", "b"])
msg = (
"This OneHotEncoder instance is not fitted yet. "
"Call 'fit' with appropriate arguments before using this "
"estimator."
)
with pytest.raises(NotFittedError, match=msg):
enc.transform(X)


@pytest.mark.parametrize("handle_unknown", ["ignore", "infrequent_if_exist"])
def test_one_hot_encoder_handle_unknown_strings(handle_unknown):
X = np.array(["11111111", "22", "333", "4444"]).reshape((-1, 1))
Expand Down Expand Up @@ -2342,3 +2330,18 @@ def test_ordinal_encoder_missing_appears_infrequent():
)
X_trans = ordinal.transform(X_test)
assert_allclose(X_trans, [[2, 1], [2, 0], [np.nan, 0], [1, 0], [0, 1]])


@pytest.mark.parametrize("Encoder", [OneHotEncoder, OrdinalEncoder])
def test_encoder_not_fitted(Encoder):
"""Check that we raise a `NotFittedError` by calling transform before fit with
the encoders.

One could expect that the passing the `categories` argument to the encoder
would make it stateless. However, `fit` is making a couple of check, such as the
position of `np.nan`.
"""
X = np.array([["A"], ["B"], ["C"]], dtype=object)
encoder = Encoder(categories=[["A", "B", "C"]])
with pytest.raises(NotFittedError):
encoder.transform(X)