Put the penguin model behind a Gradio app: sliders for the measurements, and the app says the species and how sure it is.
Apps that identify plants, birds or songs are a trained model with a simple app in front. You can make one.
A model is only useful if people can use it. Here the model is trained on all the penguins (testing was done last lesson), then a Gradio app (from Apps with Gradio, or Data detective's dashboard) asks for the measurements and shows the guess. predict_proba says how the 5 nearest penguins voted: if 3 of 5 are Chinstrap, it is 60% sure.
Make your prediction first. Help opens once you've locked it in.
neighbours = ["Gentoo", "Gentoo", "Chinstrap", "Gentoo", "Chinstrap"]
print(neighbours.count("Gentoo") / len(neighbours))
count counts how many times something is in a list. What share of the five neighbours are Gentoo?
Run it (scikit-learn takes a few seconds the first time). Move the sliders and press Submit. Find a penguin it is 100% sure about, and one it is not sure about.
import gradio as gr
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"])
def identify(bill, flipper):
mystery = pd.DataFrame([[bill, flipper]], columns=columns)
species = model.predict(mystery)[0]
sure = round(max(model.predict_proba(mystery)[0]) * 100)
return species, str(sure) + "% of the 5 nearest penguins are " + species
gr.Interface(
fn=identify,
inputs=[
gr.Slider(30, 60, value=45, step=0.5, label="Bill length (mm)"),
gr.Slider(170, 235, value=200, step=1, label="Flipper length (mm)"),
],
outputs=[gr.Textbox(label="Species"), gr.Textbox(label="How sure")],
title="Penguin identifier",
).launch()
import gradio as gr
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"])
def identify(bill, flipper):
mystery = pd.DataFrame([[bill, flipper]], columns=columns)
species = model.predict(mystery)[0]
sure = round(max(model.predict_proba(mystery)[0]) * 100)
return species, str(sure) + "% of the 5 nearest penguins are " + species
gr.Interface(
fn=identify,
inputs=[
gr.Slider(30, 60, value=45, step=0.5, label="Bill length (mm)"),
gr.Slider(170, 235, value=200, step=1, label="Flipper length (mm)"),
],
outputs=[gr.Textbox(label="Species"), gr.Textbox(label="How sure")],
title="Penguin identifier",
).launch()
Why there is no test set here.
What the vote share says.
Numbers in the wrong places.
A penguin with bill 47 and flipper 215 (very like a Gentoo) is identified as Chinstrap. Click the line that is wrong.
Add bill depth as a third measurement. Is the app surer now?
Add "bill_depth_mm" to columns, a depth parameter to identify, and depth to the mystery row.
Add a third slider: gr.Slider(13, 22, value=17, step=0.1, label="Bill depth (mm)").
When fewer than 80% of the neighbours agree, show Not sure instead of a species.
If sure < 80, return "Not sure" as the species (and still show the vote).
Add bill depth as a third measurement: in the model, the function and a slider.
Run your program. This ticks when your code does all of the above.
Say Not sure when fewer than 80% of the neighbours agree.
Run your program. This ticks when your code does all of the above.
Build an app around a model of your own: predict a penguin's sex or island from measurements you choose. (Some penguins have no sex recorded: penguins.dropna() leaves them out.) Test it first, as in Lesson 4, and put the accuracy in the app's title.
Build an app around a model of your own: choose what it predicts and from which measurements.
Run your program. This ticks when your code does all of the above.
On a computer, this file runs in real Gradio (pip install gradio scikit-learn pandas), and a teacher can host it as a Hugging Face Space for the class to try.
Students put a 5-nearest-neighbour penguin model behind a Gradio app with sliders, show how sure it is with predict_proba, then add bill depth, answer "Not sure" below 80%, and build an app around a model of their own (sex or island). About 45 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Read the unit primer first. The model is trained on all 342 penguins (the method was tested fairly in Lesson 4; for use, a model should learn from all the data). The app's function turns the slider values into a one-row table with the training columns, then predict gives the species and predict_proba the share of the 5 neighbours voting for each species (the Predict works one out by hand). The bug task puts the numbers in the wrong order: the model reads by position, so it sees a 215 mm bill and answers wrongly without any error. In the Make, penguins.dropna() drops the penguins with no sex recorded. On a computer, pip install gradio scikit-learn pandas runs the same file; a teacher can host it as a Hugging Face Space (accounts need age 13+).
Keeping columns, parameters and sliders in the same order. Ask them to read the three lists side by side.
Stretch: Show the three species' vote shares as a bar chart (gr.BarPlot with a small DataFrame), or add a Dropdown to choose the model (neighbours or tree).
Support: Give students the third slider line, and ask them to add the column and the parameter.
The optional last lesson looks at what models get wrong. The micro:bit and the computer course trains a model on movement with Teachable Machine.