Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type error with LabelEncoder.fit_transform()

I was playing with the Titanic dataset on Kaggle (https://www.kaggle.com/c/titanic/data), and I want to use LabelEncoder from sklearn.preprocessing to transform Sex, originally labeled as 'male' or 'female' into '0' or '1'. I had the following four lines of code,

import pandas as pd
from sklearn.preprocessing import LabelEncoder
df = pd.read_csv('titanic.csv')
df['Sex'] = LabelEncoder.fit_transform(df['Sex'])  

But when I ran it I received the following error message:

TypeError: fit_transform() missing 1 required positional argument: 'y'

the error comes from line 4, i.e.,

df['Sex'] = LabelEncoder.fit_transform(df['Sex']) 

I wonder what went wrong here. Although I know I could also do the transformation using map, which might be even simpler, but I still want to know what's wrong with my usage of LabelEncoder.

like image 754
Yue Zhao Avatar asked Jan 03 '23 02:01

Yue Zhao


1 Answers

Try using following link to sklearn. LabelEncoder is a utility class and need to create the object with LabelEncoder():

le = LabelEncoder()
df['Sex'] = le.fit_transform(df['Sex'])

Testing with example:

# create test series
gender = pd.Series(['male', 'female', 'male'])

le = LabelEncoder()
transformed_val = le.fit_transform(gender)

# checking result after using label encoder
print(transformed_val)

Results:

[1 0 1]
like image 176
student Avatar answered Jan 05 '23 15:01

student