← Teacher guide

What models get wrong

An optional lesson. Students find where the penguin model fails (an unseen species, impossible measurements, confident wrong answers), add a "not like any penguin" check with kneighbors, test harder questions (sex, island), and write a model card.

Machine learning with Gradio · Real tools · about 45 minutes · Open the student lesson

Classroom resources

Open these in Waypoints Prepare with one click. To share a file another way, copy its address and paste it into Load URL, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 10 min · Investigate 10 min · Modify 15 min · Make 10 min

Before class

Optional: not needed for completing the course. A classifier can only choose between the classes in its training data, so an emperor penguin (not in the data) is called Gentoo; impossible measurements still get a 100%-sure answer, because the vote share compares neighbours with each other and ignores how far away they are. model.kneighbors(...) returns the distances, which the first Modify uses to refuse far-away inputs. A fair test shows species is easiest (88% here with this split), sex harder (73%) and island hardest (64%) from bill and flipper length alone. The data comes from three islands near Palmer Station, 2007 to 2009; a model knows only that world. The info question moves to a human context (a school using past records): be ready for discussion about fairness and human review. A model card is a short honest description used in industry.

Running the lesson

  1. Predict: ask what answers the model can possibly give.
  2. Run: read the 100% sure answers aloud and ask who believes them.
  3. Make: share model cards in pairs and challenge each other's limits.
  4. Info: keep the discussion about the system, not about individual students.

The hard part (on purpose)

Separating confidence from correctness. Ask students to argue for and against trusting a 100% answer before they code the check.

When students get stuck

What you will seeA question to askThe fix
ValueError: Input contains NaN when testing sex.Are there penguins with no sex recorded?pd.read_csv("penguins.csv").dropna().
Every mystery says not like any penguin.How far is too far, and which distance is checked?distances[i][0] is the nearest; try a limit of about 10.

Answers to the checks

  1. Predict: An emperor penguin (much bigger, and a species not in the data) is measured. What does the model say?
    Gentoo. The model can only choose between Adelie, Chinstrap and Gentoo, so it picks the closest, even for a penguin unlike any it has seen.
  2. Sure but wrong: Why is the model 100% sure about a penguin 20 mm long?
    Its 5 nearest penguins are all Adelie, far away
    Its 5 nearest penguins are all Adelie, though they are far away: the vote says nothing about distance. The vote share only compares the neighbours with each other. Confidence is not the same as being right.
  3. Where the data came from: All the penguins come from three islands near one research station, from 2007 to 2009. What follows?
    It may do worse on penguins from other places
    It may do worse on penguins from other places or years, which it has never seen. A model has learned only from the world its data came from. Who collected the data, where and when, decides what it can be trusted for.
  4. Where this leads: A school wants a model that guesses which students need extra help, trained on past years' records. What should they check first?
    Whether its past data is fair to every group
    Check whether the past data treats every group fairly, how it was tested, and who checks its mistakes. Models copy the patterns in their data, including unfair ones. Real uses need fair testing, human review and a clear statement of limits.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › Too far away

import pandas as pd
from sklearn.neighbors import KNeighborsClassifier

penguins = pd.read_csv("penguins.csv")
columns = ["bill_length_mm", "flipper_length_mm"]
model = KNeighborsClassifier(n_neighbors=5)
model.fit(penguins[columns], penguins["species"])

mysteries = pd.DataFrame([[39.0, 190.0], [48.0, 195.0], [80.0, 350.0], [20.0, 120.0]], columns=columns)
guesses = model.predict(mysteries)
chances = model.predict_proba(mysteries)
distances, places = model.kneighbors(mysteries)
for i in range(len(guesses)):
    if distances[i][0] > 10:
        print(str(list(mysteries.iloc[i])) + ": not like any penguin in the data")
    else:
        print(str(list(mysteries.iloc[i])) + ": " + guesses[i] + ", " + str(round(max(chances[i]) * 100)) + "% sure")

Modify › Harder questions

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

penguins = pd.read_csv("penguins.csv").dropna()
for target in ["species", "sex", "island"]:
    X = penguins[["bill_length_mm", "flipper_length_mm"]]
    y = penguins[target]
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1)
    model = KNeighborsClassifier(n_neighbors=5).fit(X_train, y_train)
    print(target + ": " + str(round(model.score(X_test, y_test) * 100)) + "%")

Stretch and support

Stretch: Find the island model's mistakes and explain them (some species live on more than one island).

Support: Give students the kneighbors line, and ask them to write the if.

Exit ticket

Why can a model be 100% sure and still wrong?

Its confidence only compares the answers it knows (here, the votes of its nearest examples); it does not know when an input is unlike anything it has seen.

What each step is for

Where it leads

The micro:bit and the computer course trains picture models in the Pattern Trainer, and Pattern Detector projects puts one in a Python app; the same limits apply. Discuss large language models with the same three questions: what data, how tested, what it cannot do.