Challenge: Encoding Categorical Variables
To summarize the previous three chapters, here is a table showing what encoder you should use:
In this challenge, the penguins dataset (without missing values) is provided. All categorical features, including the target ('species'
column), must be encoded.
Here is a reminder of the dataset structure:
12345import pandas as pd df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a65bbc96-309e-4df9-a790-a1eb8c815a1c/penguins_imputed.csv') print(df.head())
Keep in mind that 'island'
and 'sex'
are categorical features and 'species'
is a categorical target.
Swipe to start coding
Encode all categorical features. Use one-hot encoding for the 'island'
and 'sex'
columns, and apply a label encoder (or similar target encoder) for the 'species'
column. Follow these steps to complete the encoding.
- Import
OnehotEncoder
andLabelEncoder
. - Initialize the features encoder object.
- Encode the categorical feature columns using the
feature_enc
object. - Initialize the target encoder object.
- Encode the target using the
label_enc
object.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Which encoder should I use for each column in the penguins dataset?
Can you explain the difference between OrdinalEncoder, OneHotEncoder, and LabelEncoder?
What are the next steps to encode the categorical features and target in this dataset?
Awesome!
Completion rate improved to 3.13
Challenge: Encoding Categorical Variables
Swipe to show menu
To summarize the previous three chapters, here is a table showing what encoder you should use:
In this challenge, the penguins dataset (without missing values) is provided. All categorical features, including the target ('species'
column), must be encoded.
Here is a reminder of the dataset structure:
12345import pandas as pd df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a65bbc96-309e-4df9-a790-a1eb8c815a1c/penguins_imputed.csv') print(df.head())
Keep in mind that 'island'
and 'sex'
are categorical features and 'species'
is a categorical target.
Swipe to start coding
Encode all categorical features. Use one-hot encoding for the 'island'
and 'sex'
columns, and apply a label encoder (or similar target encoder) for the 'species'
column. Follow these steps to complete the encoding.
- Import
OnehotEncoder
andLabelEncoder
. - Initialize the features encoder object.
- Encode the categorical feature columns using the
feature_enc
object. - Initialize the target encoder object.
- Encode the target using the
label_enc
object.
Solution
Thanks for your feedback!
Awesome!
Completion rate improved to 3.13single