Issue
Here is my code and the error: The columns unique values for sex are: male,female and Embarked are: S,C,Q,nan.
Code:
from sklearn import preprocessing
label_encoder = preprocessing.LabelEncoder()
def l_e(df):
df['Embarked']= label_encoder.fit_transform(df['Embarked'])
df['Sex']= label_encoder.fit_transform(df['Sex'])
train = l_e(train)
test = l_e(test)
train
Error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_17/4261505711.py in <module>
3 def l_e(df):
4 df['Sex']= label_encoder.fit_transform(df['Sex'])
----> 5 train = l_e(train)
6 test = l_e(test)
7
/tmp/ipykernel_17/4261505711.py in l_e(df)
2 label_encoder = preprocessing.LabelEncoder()
3 def l_e(df):
----> 4 df['Sex']= label_encoder.fit_transform(df['Sex'])
5 train = l_e(train)
6 test = l_e(test)
TypeError: 'NoneType' object is not subscriptable
Solution
This error comes from your variable being None
. Make sure that train
and test
actually contain a DataFrame.
Also, note that your function doesn't return anything, so you need to apply it to the two dataframes directly, since they will be modified inside of the function, in place:
l_e(train)
l_e(test)
However, it seems unlikely that you want to use the same LabelEncoder
for the two features Embarked
and Sex
. You should have one encode per feature. Furthermore, you are using fit_transform
on both the train and test set, which is also probably not what you want to do, because that means the encodings of the labels might be different between train and test. Overall, I suggest you don't actually use a function, but rewrite your code like this:
from sklearn import preprocessing
label_encoder_embarked = preprocessing.LabelEncoder()
label_encoder_sex = preprocessing.LabelEncoder()
# Preprocessing
train['Embarked'] = label_encoder_embarked.fit_transform(train['Embarked'])
train['Sex'] = label_encoder_sex.fit_transform(train['Sex'])
test['Embarked'] = label_encoder_embarked.transform(test['Embarked'])
test['Sex'] = label_encoder_sex.fit_transform(test['Sex'])
Answered By - Florent Monin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.