Challenge: Encoding Categorical Variables
To summarize the previous three chapters, here is a table showing what encoder you should use:
In this challenge, you work with the penguins dataset (no missing values). All categorical features β including the target 'species' β must be encoded for ML use.
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
You are given a DataFrame df. Encode all categorical columns:
- Import
OneHotEncoderandLabelEncoderfromsklearn.preprocessing. - Split the data into
X(features) andy(target). - Create a
OneHotEncoderand apply it to the'island'and'sex'columns inX. - Replace those original columns with their encoded versions.
- Use
LabelEncoderon the'species'column to encodey.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
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, you work with the penguins dataset (no missing values). All categorical features β including the target 'species' β must be encoded for ML use.
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
You are given a DataFrame df. Encode all categorical columns:
- Import
OneHotEncoderandLabelEncoderfromsklearn.preprocessing. - Split the data into
X(features) andy(target). - Create a
OneHotEncoderand apply it to the'island'and'sex'columns inX. - Replace those original columns with their encoded versions.
- Use
LabelEncoderon the'species'column to encodey.
Solution
Thanks for your feedback!
single