# scikit-learn OneHot returns tuples and not a vectors

First I do a label encoding to all the columns that are strings so they will be numeric. After that, I take just the columns with the labels, convert them to np array, reshape, and convert them to one-hot encoding.

The "y" is of size 900 (of floats), and in the resize I change it to (900,1) so the one hot will work.

I use scikit-learn OneHotEncoding, and when doing fir_transform the result is:

Why do I get a tuple as output and not vectors of 1 and 0?


def OneHot(self,y):
ohe = OneHotEncoder()
y = y.reshape(len(y) , 1)
y_hot = ohe.fit_transform(y)
print(y_hot)
return y_hot


Why do I get a tuple as output and not vectors of 1 and 0?

You get this because by default OneHotEncoder() uses sparse matrix representation. Hence, it transforms the elements of y into elements of type -

<1x3 sparse matrix of type '<class 'numpy.float64'>'
with 1 stored elements in Compressed Sparse Row format>


If you want the output as vectors, then just put sparse=False in OneHotEncoder()

Following is an example of the same -

from sklearn import datasets
from sklearn.preprocessing import OneHotEncoder

# Iris dataset
print("Shape of dataset - ",X.shape, y.shape)

def OneHot(y):
ohe = OneHotEncoder(sparse=False)
y = y.reshape(len(y) , 1) # you can also use y = y.reshape(-1, 1) instead
y_hot = ohe.fit_transform(y)
return y_hot

y_oh = OneHot(y)

print("Shape of One Hot Encoded y - ",y_oh.shape)
print("Single element in y - ",y_oh[0])


The code generates the following output -

Shape of dataset -  (150, 4) (150,)
Shape of One Hot Encoded y -  (150, 3)
Single element in y -  [1. 0. 0.]

• Thanks! In all the tut I watched they didn't use it, I finally used .toarray and this got me all the vectors.. but yours is looking better Commented Sep 26, 2021 at 12:17