1
$\begingroup$

This is my code, I was trying to dummy encode the first column of X using OneHotEncoder but it was showing error and the documentation page of OneHotEncoder says that it has been changed and I wasn't able to figure out how I can do it using the changed version. the data type of X is numpy.ndarray.

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
onehotencoder=OneHotEncoder(categorical_features = [0])
X[:, 0]=onehotencoder.fit_transform(X).toarray()
$\endgroup$
0
$\begingroup$

Did you try using

X = pd.get_dummies(X)

?

Otherwise make sure that your X is either a 2D Array or a dataframe for OneHotEncoding to work.

| improve this answer | |
$\endgroup$
0
$\begingroup$

There are several ways. For the just the first colum, this should work:

pd.get_dummies(X.iloc[:,0])

If you want to explore better ways to encode features you can always use https://contrib.scikit-learn.org/categorical-encoding/index.html

It has the following parameter that should allow you to encode the columns that you want:

cols: list a list of columns to encode, if None, all string columns will be encoded.

| improve this answer | |
$\endgroup$

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