Joe
Joe

Reputation: 123

'OneHotEncoder' object has no attribute 'get_feature_names'

I am trying to extract the features using the get_feature_names function of the OneHotEncoder object of scikit learn but its is throwing me an error saying "'OneHotEncoder' object has no attribute 'get_feature_names'".

Below is the code snippet

# Creating the object instance for label encoder
encoder = OneHotEncoder(sparse=False)
onehot_encoded = encoder.fit_transform(df[data_column_category])
onehot_encoded_frame = pd.DataFrame(onehot_encoded,columns = encoder.get_feature_names(data_column_category))

Upvotes: 12

Views: 27778

Answers (3)

Liz
Liz

Reputation: 453

Apparently, it has been renamed to get_feature_names_out.

Upvotes: 18

Surya R. Giri
Surya R. Giri

Reputation: 1

Use get_feature_names_out instead of get_feature_names. Look at: https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html

Upvotes: 0

DaveR
DaveR

Reputation: 2358

That feature was introduced recently, so you might just need to update your sklearn version.

You can do it as follows:

pip install -U scikit-learn

Upvotes: 1

Related Questions