Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to keep column's names after one hot encoding sklearn?

I am working on the titanic kaggle competition, to deal with categorical data I’ve splited the data into 2 sets: one for numerical variables and the other for categorical variables. After working with sklearn one hot encoding on the set with categorical variables I tried the regroup the two datasets but since the categorical set is an ndarray and the other one is a dataframe I used:

np.hstack((X_train_num, X_train_cat))

which works perfectly but I no longer have the names of my variables.

Is there another way to do this while maintaining the names of the variables without using pd.get_dummies()?

Thanks

like image 569
user2486276 Avatar asked Jan 02 '23 09:01

user2486276


2 Answers

Try

X_train = X_train_num.join(
   pd.DataFrame(X_train_cat, X_train_num.index).add_prefix('cat_')
)
like image 124
piRSquared Avatar answered Jan 05 '23 16:01

piRSquared


Well, as you stated in your question, there's pd.get_dummies, which I think is the best choice here. Having said that, you could use

pd.concat([X_train_num, pd.DataFrame(X_train_cat, index=X_train_num.index)], axis=1)

If you like, you could give also useful column names with

pd.concat([X_train_num, pd.DataFrame(X_train_cat, index=X_train_num.index, columns=cols)], axis=1)

and cols can be whatever list of strings you want (of the appropriate length).

like image 27
Ami Tavory Avatar answered Jan 05 '23 17:01

Ami Tavory