Reputation: 13
Building pipelines with onehotencoding and when fitting and transforming to training/test set and converting into data frame it results in the features not having names. Is there any way to get names for each encoded feature?
# Numerical column transformer
num_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='mean')),
('scaler', StandardScaler())
])
# Categorical column transformer
cat_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='most_frequent')),
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
# Preprocessing pipeline
preprocessor = ColumnTransformer(
transformers=[
('num', num_transformer, numerical_cols),
('cat', cat_transformer, categorical_cols)
])
# Fitting the data and transforming the training & test set
X_train_preprocessed = preprocessor.fit_transform(X_train)
test_preprocessed = preprocessor.fit_transform(test)
Upvotes: 1
Views: 612
Reputation: 2936
You can access transformers using attribute named_transformers_
of ColumnTransformer
. You have 2 transformers named 'num'
and 'cat'
, so preprocessor.named_transformers_['cat']
gives you access to your cat_transformer
. Then using named_steps
attribute of Pipeline
you can access your OneHotEncoder
named 'onehot'
and its categories_
attribute:
X = [['Male', 1], ['Female', 3], ['Female', 2]]
preprocessor.fit_transform(X)
Out[6]:
array([[-1.22474487, 0. , 1. ],
[ 1.22474487, 1. , 0. ],
[ 0. , 1. , 0. ]])
preprocessor.named_transformers_['cat'].named_steps['onehot'].categories_
Out[7]: [array(['Female', 'Male'], dtype=object)]
Upvotes: 1