Skip to content

ENH Adds encoded_missing_value to OrdinalEncoder - #21988

Merged
jeremiedbb merged 9 commits into
scikit-learn:mainfrom
thomasjpfan:ordinal_encoder_nan_rb
Mar 23, 2022
Merged

jeremiedbb merged 9 commits into
scikit-learn:mainfrom
thomasjpfan:ordinal_encoder_nan_rb

Conversation

@thomasjpfan

Copy link
Copy Markdown
Member

Reference Issues/PRs

Related to #21967

What does this implement/fix? Explain your changes.

This PR adds encoded_missing_value to OrdinalEncoder. Similar to unknown_value, encoded_missing_value is not allowed to be one of the encoding used by a known category.

CC @glemaitre

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick suggestion for a usability improvement in the error message both other than that, LGTM!

Comment thread sklearn/preprocessing/_encoders.py Resolved
@glemaitre
glemaitre self-requested a review December 23, 2021 10:55
@glemaitre

Copy link
Copy Markdown
Member

Maybe a short example in the docstring:

    By default, :class:`OrdinalEncoder` is lenient towards missing values by
    propagating them.

    >>> import numpy as np
    >>> X = [['Male', 1], ['Female', 3], ['Female', np.nan]]
    >>> enc.fit_transform(X)
    array([[ 1.,  0.],
           [ 0.,  1.],
           [ 0., nan]])

    You can use the parameter `encoded_missing_value` to encode missing values.

    >>> enc.set_params(encoded_missing_value=-1).fit_transform(X)
    array([[ 1.,  0.],
           [ 0.,  1.],
           [ 0., -1.]])

@glemaitre

Copy link
Copy Markdown
Member

In the User Guide l.540 of preprocessing.rst, I think that we can add/modify with:

By default, :class:`OrdinalEncoder` will also passthrough missing values that are
indicated by `np.nan`.

    >>> enc = preprocessing.OrdinalEncoder()
    >>> X = [['male'], ['female'], [np.nan], ['female']]
    >>> enc.fit_transform(X)
    array([[ 1.],
           [ 0.],
           [nan],
           [ 0.]])

:class:`OrdinalEncoder` provides a parameter `encoded_missing_value` to encode
the missing values without the need to create a pipeline and using
:class:`~sklearn.impute.SimpleImputer`.

    >>> enc = preprocessing.OrdinalEncoder(encoded_missing_value=-1)
    >>> X = [['male'], ['female'], [np.nan], ['female']]
    >>> enc.fit_transform(X)
    array([[ 1.],
           [ 0.],
           [-1.],
           [ 0.]])

The above processing is equivalent to the following pipeline::

    >>> from sklearn.pipeline import Pipeline
    >>> from sklearn.impute import SimpleImputer
    >>> enc = Pipeline(steps=[
    ...     ("encoder", preprocessing.OrdinalEncoder()),
    ...     ("imputer", SimpleImputer(strategy="constant", fill_value=-1)),
    ... ])
    >>> enc.fit_transform(X)
    array([[ 1.],
           [ 0.],
           [-1.],
           [ 0.]])

@thomasjpfan thomasjpfan added this to the 1.1 milestone Mar 3, 2022

@jeremiedbb jeremiedbb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants