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:

out put


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)
        return y_hot

1 Answer 1


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
X, y = datasets.load_iris(return_X_y=True)
print("Shape of dataset - ",X.shape, y.shape)

# Your code
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.]
  • $\begingroup$ 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 $\endgroup$ Sep 26, 2021 at 12:17
  • $\begingroup$ @JamseGoldman Please consider accepting the answer if it answers your question. :) $\endgroup$ Sep 26, 2021 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.