You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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?
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 !
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.)
Activity
olologin commentedon Nov 28, 2015
You can use MultiLabelBinarizer to achieve similar results, but again, it will work only on iterables over iterables.
i.e.
MechCoder commentedon Dec 1, 2015
Why can you not just use a LabelBinarizer, unless I'm missing something?
Not allowing 1-D inputs was explicitly done here (#5152)
hlin117 commentedon Dec 1, 2015
So, the LabelBinarizer works to some extent:
But I'd expect a OneHotEncoder to return something like this:
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.
hlin117 commentedon Dec 1, 2015
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 fory.My rationale for discouraging people from using OneHotEncoders for
yis because scikit-learn models usually accept 1Dy. So a natural workflow would to pass a 1Dyinto OneHotEncoders - if such a workflow were correct. But really, the 1Dyshould be passed into LabelBinarizers.Is a PR welcome to open up this sort of documentation?
MechCoder commentedon Dec 2, 2015
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 !
hlin117 commentedon Dec 2, 2015
Yeah, that's true. Maybe it's just the strange workflow that I'm working with which requires the output
yto be a 2D array =XOh man, didn't see this:
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.)
amueller commentedon Oct 27, 2016
closed in #7315