Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Encoding Categorical Variables | Section
Machine Learning Foundations with Scikit-Learn
Avsnitt 1. Kapitel 13
single

single

bookChallenge: Encoding Categorical Variables

Svep för att visa menyn

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.

12345
import 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())
copy

Keep in mind that 'island' and 'sex' are categorical features and 'species' is a categorical target.

Uppgift

Svep för att börja koda

You are given a DataFrame df. Encode all categorical columns:

  1. Import OneHotEncoder and LabelEncoder from sklearn.preprocessing.
  2. Split the data into X (features) and y (target).
  3. Create a OneHotEncoder and apply it to the 'island' and 'sex' columns in X.
  4. Replace those original columns with their encoded versions.
  5. Use LabelEncoder on the 'species' column to encode y.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 13
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt