Error on ‘inverse_transform’ of LabelEncoder

If you get the following error when calling inverse_transform on sklearn.preprocessing.LabelEncoder

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

You could be passing in a numpy.ndarray of strings, not numeric values.

Here’s a snippet to encode and decode a column from the Titanic training set:

from sklearn.preprocessing import LabelEncoder

encoder = LabelEncoder()
sex_cat = train_df["Sex"]
sex_cat_encoded = encoder.fit_transform(sex_cat)
print(encoder.inverse_transform(sex_cat_encoded))
# print(encoder.inverse_transform(sex_cat)) This will give you the error

This entry was posted in Programming and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *