You can access the names of the features generated by OneHotEncode in ColumnTransformer using the get_feature_names()
method. Here's an example:
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder
# Define ColumnTransformer to apply OneHotEncoder to categorical features
preprocessor = ColumnTransformer(transformers=[('cat', OneHotEncoder(), ['gender', 'education'])])
# Define pipeline to apply preprocessor and other steps
pipeline = Pipeline(steps=[('preprocessor', preprocessor), ('classifier', LogisticRegression())])
# Fit pipeline to data
pipeline.fit(X_train, y_train)
# Get the names of the generated features
feature_names = pipeline.named_steps['preprocessor'].transformers_[0].named_steps['onehotencoder'].get_feature_names(['gender', 'education'])
print(feature_names)
In the example above, get_feature_names()
is used to get the names of the generated features for the columns gender
and education
. The resulting feature_names
list will contain the names of the generated features in the order they were created by the OneHotEncoder.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-06-20 11:00:00 +0000
Seen: 1 times
Last updated: Feb 09
How can I deal with Expression.Error related to a column in Power Query?
How can you implement pagination in Oracle for the LISTAGG() function?
What is the process for implementing a FutureBuilder on an OnTap function in Flutter?
How can we require users to be logged in before they can access the root folders in WordPress?
In SCSS, what is the method for grouping and reusing a set of classes and styles?
How can popen() be used to direct streaming data to TAR?
How does iOS retrieve information from a BLE device?
How can Django Admin accommodate a variety of formats and locales for its input fields?