Ship the Model
Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 54 of 58.
The test penguins have waited the whole project. Fit the tuned pipeline on the training rows, then score it on the test penguins once: the honest number.
Then see where it slips. A clinic's model names healthy, cold or flu; rows are the truth, columns its guesses:
A new penguin is a one-row table with the training columns. predict names it, and predict_proba(new).max() is how sure: the winning class's share, for k-NN the neighbors that agree.
Challenge
MediumThe finish line. model is your tuned pipeline; the starter reads a new penguin into new_penguin.
- Fit the model on the training rows. Print its test accuracy (3 decimals) and how many test penguins it named right.
- Draw the confusion matrix of the test predictions on
axand show it. - Name the new penguin and how sure the model is, in percent.
For a colony at another station:
Test accuracy: 0.960 (72 of 75 penguins)
New penguin: Gentoo (86% sure)and its matrix:
Then Coddy reports yours. The star from Meet the Penguins measured 47.2, 18.0, 194 and 3650, on Dream, female. Run your program with her: who is she?
Try it yourself
import pandas as pd
import matplotlib.pyplot as plt
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
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import ConfusionMatrixDisplay
# 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"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, stratify=y, random_state=0)
# From Prepare the Data: the two lanes
number_steps = make_pipeline(SimpleImputer(strategy="median"), StandardScaler())
text_steps = make_pipeline(SimpleImputer(strategy="constant", fill_value="unknown"), OneHotEncoder())
prep = ColumnTransformer([("numbers", number_steps, numbers), ("words", text_steps, text)])
# From Choose a Model and Tune the Winner: k-NN with k = 7
model = make_pipeline(prep, KNeighborsClassifier(n_neighbors=7))
# A new penguin from the field, as a one-row table with the same columns
new_penguin = pd.DataFrame({
"bill_length_mm": [float(input())],
"bill_depth_mm": [float(input())],
"flipper_length_mm": [float(input())],
"body_mass_g": [float(input())],
"island": [input()],
"sex": [input()],
})
fig, ax = plt.subplots()
ax.set_title("Test Penguins")
# 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