RasK
RasK

Reputation: 51

TypeError while using label encoder

I am using the Beers dataset in which I want to encode the data with datatype 'object'. Following is my code.

from sklearn import preprocessing

df3 = BeerDF.select_dtypes(include=['object']).copy()

label_encoder = preprocessing.LabelEncoder()
df3 = df3.apply(label_encoder.fit_transform)

The following error is occurring.

TypeError: Encoders require their input to be uniformly strings or numbers. Got ['float', 'str']

Any insights are helpful!!!

Upvotes: 0

Views: 620

Answers (2)

Muhammad Hassan
Muhammad Hassan

Reputation: 4229

Use:

df3 = df3.astype(str).apply(label_encoder.fit_transform)

Upvotes: 1

Raklet57
Raklet57

Reputation: 101

From the TypeError, it seems that the column you want to transform into label has two different data types (dtypes), in your case string and float which raises an error. To avoid this, you have to modify your column to have a uniform dtype (string or float). For example in Iris classification dataset, class=['Setosa', 'Versicolour', 'Virginica'] and not ['Setosa', 3, 5, 'Versicolour']

Upvotes: 0

Related Questions