← Teacher guides

Teacher guide: Machine learning with Gradio

How a computer learns from examples: rules by hand, a nearest-neighbour model written yourself, fair testing, then scikit-learn and a model behind a Gradio app, with what models get wrong.

Open the student course

Each lesson works two ways. Students can work through the online tutorial at their own pace (it checks their answers and saves their progress in their browser), or you can teach it to the whole class from the slides, then send students into the tutorial for the Modify and Make steps.

Every lesson guide has plain-language notes, the answers to every check, a solution for every task, and slides and a worksheet for Waypoints Prepare. These guide pages are not linked from student pages, but they are not secret either: keep printed Teacher editions for answers you do not want students to find.

Whole-course cheat sheet: Open to print (every command, grouped by unit: a handout for the final challenge)

Models by hand

Rules, then a nearest-neighbour model you write yourself, and how to test a model fairly.

Models by hand in 5 minutes (read before teaching this unit)

The idea in plain words

A model takes measurements and gives an answer: here, a penguin's bill and flipper length in, its species out. Its accuracy is how often it is right.

  • Rules (Lesson 1): if flipper > 206: return "Gentoo". A person chooses the numbers by looking at the data. On the Palmer Penguins they get 95% right.
  • Nearest neighbour (Lesson 2): keep every example, and answer with the species of the most similar one. Similarity is a distance: Pythagoras on the two measurements. Nobody writes rules; the examples are the knowledge. A small distance means a confident guess.
  • A fair test (Lesson 3): shuffle the data, train on most of it and test on the rest. Tested on its own training data, nearest neighbour scores 100% (each penguin finds itself), which proves nothing; on unseen penguins it scores about 95%.

Why programmers use them

  • Machine learning finds patterns in examples when rules are too hard to write: recognising photos, sounds, handwriting or movement.
  • Fair testing is how anyone knows whether a model works, from school projects to medical tools.

An everyday picture, and where it breaks

Nearest neighbour is identifying a bird by finding the most similar picture in a field guide. A fair test is a practice exam with questions you have not seen. It breaks because the model never knows when nothing in the guide matches: it always picks the closest picture.

Try it yourself (5 minutes)

  1. Run Lesson 1's rules and change 206 to 200.
  2. In Lesson 2, try a mystery penguin of 50, 196 and read the distance.
  3. In Lesson 3, compare the two scores, then change the seed.

Questions students ask

  • Is nearest neighbour really machine learning? Yes: it learns from examples, and scikit-learn has it (KNeighborsClassifier).
  • Why do the flippers matter more? Their numbers are bigger, so differences in them dominate the distance (Lesson 4 comes back to this).
  • Where does the data come from? Scientists at Palmer Station, Antarctica (Gorman, Williams and Fraser 2014), shared free for teaching.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown

Real tools

The same ideas in scikit-learn, then a model behind an app, and what models get wrong.

Real tools in 5 minutes (read before teaching this unit)

The idea in plain words

scikit-learn is the standard Python library for machine learning. Everything built by hand has a line in it: train_test_split (shuffle and split), KNeighborsClassifier (nearest neighbour), fit (learn from the training examples), score (accuracy on given data), predict (guess new examples). A decision tree learns if rules from the data, like Lesson 1's but chosen by the computer.

A model becomes useful in an app: a Gradio function turns slider values into a one-row table with the training columns, and the model predicts. predict_proba says how the 5 nearest penguins voted, which tells the user how sure it is.

Limits. A model can only answer with what it was trained on (an emperor penguin is called Gentoo), and it can be 100% sure and wrong. It knows the world its data came from (three islands, 2007 to 2009). A model card says what a model does, how well on a fair test, and where not to trust it.

Why programmers use them

  • Libraries are tested and fast, so data scientists spend their time on data and questions, not on writing distance loops.
  • Apps put models in people's hands; model cards and fair tests keep them honest.

An everyday picture, and where it breaks

scikit-learn is a well-made toolkit: you still need to know which tool does what, and to check the result. It breaks if you trust it blindly: the same fit and score on the wrong data give a convincing wrong answer.

Try it yourself (5 minutes)

  1. Run Lesson 4 and match each line with Lessons 2 and 3.
  2. Add body mass to the measurements and watch nearest neighbour get worse (grams swamp the distance).
  3. In Lesson 5, find measurements the app is only 60% sure about.

Questions students ask

  • Why train the app's model on all the data? Testing showed the method works; for use, it should learn from every example.
  • Why does it take a while to start? The browser downloads scikit-learn the first time (about 19 MB), then keeps it.
  • Is this how big AI works? The same ideas (examples, fair tests, honest limits), with far more data and bigger models.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown