I have a numpy array that looks like the following:
array([[0],[1],[1]])
And I want it to be represented as the one hot encoded equivalent:
array([[1,0],[0,1],[0,1]])
Any body have any ideas? I tried using sklearn.preprocessing.LabelBinarizer but this just re-produces the input.
Thanks.
EDIT
As requested, here is the code using LabelBinarizer
from sklearn.preprocessing import LabelBinarizer
train_y = np.array([[0],[1],[1]])
lb = LabelBinarizer()
lb.fit(train_y)
label_vecs = lb.transform(train_y)
Output:
array([[0],[1],[1]])
Note that it does state in the documentation 'Binary targets transform to a column vector'
To use
sklearn
, it seems we could useOneHotEncoder
, like so -Sample inputs, outputs -
Another approach with
initialization
-Another with
broadcasting
-