Feature names from OneHotEncoder Feature names from OneHotEncoder python-3.x python-3.x

Feature names from OneHotEncoder


You can pass the list with original column names to get_feature_names:

encoder.get_feature_names(['Sex', 'AgeGroup'])

will return:

['Sex_female', 'Sex_male', 'AgeGroup_0', 'AgeGroup_15', 'AgeGroup_30', 'AgeGroup_45', 'AgeGroup_60', 'AgeGroup_75']


column_name = encoder.get_feature_names(['Sex', 'AgeGroup'])one_hot_encoded_frame =  pd.DataFrame(train_X_encoded, columns= column_name)


Thanks for a nice solution. @NursnaazThe sparse matrix needs to convert into a dense matrix.

column_name = encoder.get_feature_names(['Sex', 'AgeGroup'])one_hot_encoded_frame =  pd.DataFrame(train_X_encoded.todense(), columns= column_name)