TransWikia.com

How do i get dummies for this dataset

Data Science Asked by Adetola Aremu on April 20, 2021

I am using an udemy course for MachineLearning and I am trying to form a dummy for my variable the column is Country

I want to change to France Germany Spain

- France                             1        0       0
- Spain                              0        0       1
- Germany                            0        1       0  etc

I tried this but I got this error

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
X[:, 0] = labelencoder_X.fit_transform(X[:, 0])
onehotencoder = OneHotEncoder(categorical_values = [0])
X = onehotencoder.fit_transform(X).toarray()
Traceback (most recent call last):

  File "<ipython-input-9-f7654080cf6a>", line 4, in <module>
    onehotencoder = OneHotEncoder(categorical_values = [0])

TypeError: __init__() got an unexpected keyword argument 'categorical_values'

3 Answers

Its easier to perform this by method get_dummies:

X_enc = pd.get_dummies(X)

Reference:

Answered by fuwiak on April 20, 2021

As the error says: there is no categorical_values parameter for OneHotEncoder. It was removed at the same time that OneHotEncoder was extended to deal with strings directly, and you may want to use ColumnTransformer for selecting out the categorical column(s). For example, https://datascience.stackexchange.com/a/57383/55122

Answered by Ben Reiniger on April 20, 2021

col = []
for c in df.columns:
    if df[c].dtypes=='object':
        col.append(c)

df_dummies = pd.get_dummies(df , columns=col, drop_first=True) ## get dummies part

It is a good practice to use the drop_first parameter as it would avoid the model getting overfitted.

Answered by kappil c on April 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP