How to Assign Feature Names in a OneHotEncoder through Column Transformer

Viewed 39

I understand that if I run a OneHotEncoder by itself, I am able to change the feature names that it generates from x1_1, x1_2, etc. by calling .get_feature_names e.g.:

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

will change x1_1, x2_2 to AgeGroup_1, AgeGroup_2 etc.

However, if I run the OneHotEncoder as one of a few transformations in a ColumnTransformer, how would I be able to change set prefix?

  1. Is there a way to set this prefix before the encoding even starts, e.g. within the initialization parameters to OneHotEncoder, or
  2. somehow in-line with the ColumnTransformer, or
  3. without doing some string parsing replacement parsing on the columns after the fit_transform?
0 Answers
Related