Menu
CoddyTech

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 3 by 3 confusion matrix of a clinic's model on 60 test patients, healthy, cold and flu: the diagonal holds 20, 14 and 17 right answers, and 5 colds were called flu

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 icon

Challenge

Medium

The finish line. model is your tuned pipeline; the starter reads a new penguin into new_penguin.

  1. Fit the model on the training rows. Print its test accuracy (3 decimals) and how many test penguins it named right.
  2. Draw the confusion matrix of the test predictions on ax and show it.
  3. 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:

A confusion matrix titled Test Penguins for another station: 29 Adelie, 16 Chinstrap and 27 Gentoo named right, 1 Adelie called Chinstrap and 2 Chinstrap called Adelie

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
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