Menu
CoddyTech

Prepare the Data

Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 51 of 58.

A model reads only complete rows of numbers. The penguins have gaps and two text columns, so every column goes down a lane:

A diagram of one training penguin. Its four measurements go down a blue lane, fill with median then scale, and come out as -0.79, 0.00, -0.05 and -0.90. Its island Biscoe and its missing sex go down an orange lane, fill unknown then one-hot, and come out as six 0 or 1 columns

Text gaps get the word "unknown": a penguin of unrecorded sex earns its own column instead of a guess.

text_steps = make_pipeline(
    SimpleImputer(strategy="constant", fill_value="unknown"),
    OneHotEncoder())

Fit the lanes on the training rows only. A median that has seen the test penguins would leak them into training.

challenge icon

Challenge

Medium

The penguins need the lanes from the picture. The starter splits them and reads a row number.

  1. Build prep, a ColumnTransformer that sends numbers down the number lane and text down the text lane.
  2. Fit it on the training rows and transform them in one call.
  3. Print the size and gaps before, the size after, then that row prepared, to 2 decimals.

For another colony and row 3:

Before: 180 penguins, 6 columns, 9 gaps
After: 180 penguins, 10 columns
Penguin 3: [-1.12  0.47 -0.83 -0.6   0.    1.    0.    0.    1.    0.  ]

Row 14 is a penguin nobody could measure. What does it become?

Try it yourself

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import make_pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, OneHotEncoder

# The colony from Meet the Penguins, its features and its label
penguins = pd.read_csv("penguins.csv")
numbers = ["bill_length_mm", "bill_depth_mm", "flipper_length_mm", "body_mass_g"]
text = ["island", "sex"]
X = penguins[numbers + text]
y = penguins["species"]

# Split first: the test penguins stay locked away until Ship the Model
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, stratify=y, random_state=0)
row = int(input())

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Supervised Learning

Practice on your own: Online Python compiler