0
$\begingroup$

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 "", line 4, in onehotencoder = OneHotEncoder(categorical_values = [0])

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

$\endgroup$
0
$\begingroup$

Its easier to perform this by method get_dummies:

X_enc = pd.get_dummies(X)

Reference:

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

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

| improve this answer | |
$\endgroup$
0
$\begingroup$
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.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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