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:
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
MediumThe penguins need the lanes from the picture. The starter splits them and reads a row number.
- Build
prep, aColumnTransformerthat sendsnumbersdown the number lane andtextdown the text lane. - Fit it on the training rows and transform them in one call.
- 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Supervised Learning
1Your First Model
Learning from ExamplesTrain and Test SetsFit and PredictScoring a ModelRecap: Seed Sorter2Nearest Neighbors
How k-NN DecidesChoosing kDecision BoundariesScaling for NeighborsRecap: Wine Neighbors5Measuring Classifiers
The Confusion MatrixPrecision and RecallThe F1 ScoreImbalanced ClassesROC CurvesRecap: Screening Test11Project: Penguin Species
Meet the PenguinsPrepare the DataChoose a ModelTune the WinnerShip the ModelPractice on your own: Online Python compiler