简体   繁体   中英

Create Dummy Variable with Column Transformer using sklearn

I've been trying to create a DL model for a practice purpose using ANN. I've a fake bank's customer data in which there are two categorical variable ie gender and country.

I encode both the columns using LabelEncoder but not able to create dummy variables for country columns. In coutry there are three countries ie France, Germany and Spain.

Error I got:

ValueError: not enough values to unpack (expected 3, got 2)

My Code:

# Encodeing categorical data
# for country column
labelencoder_X_1 = LabelEncoder()
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])

# for gender column
labelencoder_X_2 = LabelEncoder()
X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2])

ct = ColumnTransformer(
transformers=[
    ("dummy_var",        # Just a name
     OneHotEncoder( # The transformer class
     categories=[[1]])            # The column(s) to be applied on.
     )
], remainder='passthrough')
X = ct.fit_transform(X).toarray()
X = X[:, 1:]
# print(X)

PS: I use Pycharm and novice in Deep Learning.

Thanks in Advance!

I think you mistake is giving the column name inside the OneHotEncoder. It has to be given for ColumnTransformer .

Try this!

ct = ColumnTransformer(
transformers=[
    ("dummy_var",        # Just a name
     OneHotEncoder(), # The transformer class
     [1]       # The column(s) to be applied on.     
     )
], remainder='passthrough')

Note: You don't have to apply labelEncoder before OneHotEncoder . You apply OneHotEncoder directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM