Skip to content

Letting OneHotEncoder encode y? #5930

Closed

Description

@hlin117

I think the original use case for OneHotEncoders were to encode features, but it would be nice if they were able to encode y's as well. The current functionality is as follows:

>>> from sklearn.preprocessing import OneHotEncoder
>>> y = [0, 1, 1, 0]
>>> transformed = OneHotEncoder().fit_transform(y).toarray()
/usr/local/lib/python2.7/site-packages/sklearn/utils/validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
  DeprecationWarning)
>>> transformed
array([[ 1.,  1.,  1.,  1.]])

Right now it's interpreting y as a single sample; the desired behavior is by letting y = np.array([0, 1, 1, 0]).reshape(-1, 1), and then running the OneHotEncoder.

What do you guys think?

Activity

  1. olologin commented on Nov 28, 2015

    @olologin
    Contributor

    You can use MultiLabelBinarizer to achieve similar results, but again, it will work only on iterables over iterables.
    i.e.

    from sklearn.preprocessing import MultiLabelBinarizer
    mlb = MultiLabelBinarizer()
    mlb.fit([[0],[1],[1],[0]])
    mlb.transform([[0],[1],[1],[0]])
    array([[1, 0],
           [0, 1],
           [0, 1],
           [1, 0]])
    
  2. MechCoder commented on Dec 1, 2015

    @MechCoder
    Member

    Why can you not just use a LabelBinarizer, unless I'm missing something?
    Not allowing 1-D inputs was explicitly done here (#5152)

  3. hlin117 commented on Dec 1, 2015

    @hlin117
    ContributorAuthor

    So, the LabelBinarizer works to some extent:

    >>> from sklearn.preprocessing import LabelBinarizer
    >>> y = [0, 1, 1, 0]
    >>> transformed = LabelBinarizer().fit_transform(y)
    >>> transformed
    array([[0],
           [1],
           [1],
           [0]])
    

    But I'd expect a OneHotEncoder to return something like this:

    array([[1, 0],
           [0, 1],
           [0, 1],
           [1, 0]])
    

    PS: I didn't know LabelBinarizer nor MultiLabelBinarizer existed. A separate PR should also link the OneHotEncoder page to the LabelBinarizer page, and maybe to the LabelEncoder page.

  4. hlin117 commented on Dec 1, 2015

    @hlin117
    ContributorAuthor

    Maybe a better idea would be to state on the OneHotEncoder docs that OneHotEncoders are meant to be used for features (X), and to use LabelBinarizers for y.

    My rationale for discouraging people from using OneHotEncoders for y is because scikit-learn models usually accept 1D y. So a natural workflow would to pass a 1D y into OneHotEncoders - if such a workflow were correct. But really, the 1D y should be passed into LabelBinarizers.

    Is a PR welcome to open up this sort of documentation?

  5. MechCoder commented on Dec 2, 2015

    @MechCoder
    Member

    The reason why it does not produce the expected result for a LabelBinarizer for binary y is that because it is kind of implied what the second column should be. For example, see https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/linear_model/logistic.py#L634

    Well, the documentation in the classes already states that OneHotEncoder is meant for "features" and LabelBinarizer is meant for "labels". But if you think you could make it clearer, please go ahead !

  6. hlin117 commented on Dec 2, 2015

    @hlin117
    ContributorAuthor

    The reason why it does not produce the expected result for a LabelBinarizer for binary y is that because it is kind of implied what the second column should be. For example, see https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/linear_model/logistic.py#L634

    Yeah, that's true. Maybe it's just the strange workflow that I'm working with which requires the output y to be a 2D array =X

    Well, the documentation in the classes already states that OneHotEncoder is meant for "features" and LabelBinarizer is meant for "labels". But if you think you could make it clearer, please go ahead !

    Oh man, didn't see this:

    Encode categorical integer features using a one-hot aka one-of-K scheme.

    To be fair though, I guess it doesn't discuss how to encode labels. I think a good PR would be to refer users to the other encoders to do one hot encoding for labels. (I'll add this into my bucket list of things to do after my latest research sprint.)

  7. added
    Easy
    and removed on Aug 31, 2016
  8. amueller commented on Oct 27, 2016

    @amueller
    Member

    closed in #7315

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationEasy

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions