← Machine learning with Gradio

The same model in scikit-learn

Build the nearest-neighbour model and the fair test again with scikit-learn, the library data scientists use, then compare it with a decision tree.

Everything you built by hand has a name in scikit-learn, a library used in science, banks and hospitals. Now you know what it does inside.

scikit-learn is a Python library of machine-learning models. It does in a few lines what you wrote by hand: train_test_split shuffles and splits, KNeighborsClassifier(n_neighbors=1) is your nearest-neighbour model, fit gives it the training examples, score tests it, and predict guesses a new penguin. The data is read with pandas (from Data detective): X is the measurements and y is the answers. It takes several seconds to load the first time.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

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

penguins = pd.read_csv("penguins.csv")
X = penguins[["bill_length_mm", "flipper_length_mm"]]
y = penguins["species"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1)

model = KNeighborsClassifier(n_neighbors=1)
model.fit(X_train, y_train)
print(model.score(X_train, y_train))

The model is scored on X_train and y_train. What did you find last lesson about testing on training data?

What does it print?

RunRun it and compare the result with your prediction.

Run

Run it (wait for scikit-learn to load). Then change the mystery penguin's measurements and run again.

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

penguins = pd.read_csv("penguins.csv")
X = penguins[["bill_length_mm", "flipper_length_mm"]]
y = penguins["species"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1)

model = KNeighborsClassifier(n_neighbors=1)
model.fit(X_train, y_train)
print("Test accuracy: " + str(round(model.score(X_test, y_test) * 100)) + "%")

mystery = pd.DataFrame([[47.0, 215.0]], columns=X.columns)
print("Mystery penguin: " + model.predict(mystery)[0])
Try it yourselfYour edits are saved in this browser.

InvestigateTrace how each line of code connects to what you see.

Investigate

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

penguins = pd.read_csv("penguins.csv")
X = penguins[["bill_length_mm", "flipper_length_mm"]]
y = penguins["species"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1)

model = KNeighborsClassifier(n_neighbors=1)
model.fit(X_train, y_train)
print("Test accuracy: " + str(round(model.score(X_test, y_test) * 100)) + "%")

mystery = pd.DataFrame([[47.0, 215.0]], columns=X.columns)
print("Mystery penguin: " + model.predict(mystery)[0])

The same four steps.

Why the mystery penguin needs them.

A score that is too good.

Match each scikit-learn line with what you wrote by hand.

train_test_split(X, y, test_size=0.25, random_state=1)
KNeighborsClassifier(n_neighbors=1)
model.fit(X_train, y_train)
model.score(X_test, y_test)

Why is the mystery penguin made with columns=X.columns?

This says the model is 100% accurate. Click the line that is wrong.

ModifyChange the code on purpose and check the result.

Modify

Let the 5 nearest penguins vote instead of 1. Better, worse or the same?

Need a hint?

Change n_neighbors=1 to n_neighbors=5: the 5 nearest penguins vote.

A decision tree learns if rules from the data, like the ones you wrote in Lesson 1 but chosen by the computer. Train one on the same data and compare.

Need a hint?

from sklearn.tree import DecisionTreeClassifier, then tree = DecisionTreeClassifier(max_depth=3, random_state=1).

Another hint

fit it on the same training data and score it on the same test data.

Try it yourselfYour edits are saved in this browser.

Let the 5 nearest penguins vote, and compare the test accuracy.

    Run your program. This ticks when your code does all of the above.

    Train a decision tree on the same data, and compare its test accuracy.

      Run your program. This ticks when your code does all of the above.

      MakeBuild something new using what you have learned. Expect a few tries; that is normal.

      The best measurements

      Which measurements make the best model? Try at least three choices of columns (add bill_depth_mm and body_mass_g), test each one fairly with both models, and report the best. If one choice makes the neighbours model much worse, think about last lesson's question on bigger numbers.

      Try it yourselfYour edits are saved in this browser.

      Try at least three choices of measurements, test each fairly, and report the best.

        Run your program. This ticks when your code does all of the above.

        Info

        Where this leads

        Next: put the model behind a Gradio app, so anyone can use it.

        Adding body mass (in grams, around 4000) made the nearest-neighbour model worse, but not the tree. Why?

        For teachers

        Students rebuild nearest neighbour and the fair test with scikit-learn (train_test_split, fit, score, predict), match each line to their hand-built version, compare 5 neighbours and a decision tree, and search for the best measurements. About 45 minutes.

        Open the teacher guide: answers, solutions, slides and a worksheet.

        Students will be able to

        Before class

        Read the unit primer first. scikit-learn loads the first time a program imports it (several seconds; about 19 MB, cached by the browser afterwards). X is a pandas table of measurements and y the species; train_test_split(..., random_state=1) shuffles and splits; fit stores the training examples (for nearest neighbour) or learns rules (for a tree); score is accuracy on the given data (the bug task scores on training data: 100%); predict needs a table with the same columns. Results: 94% test accuracy with 1 neighbour, 93% with 5, 93% for a tree of depth 3. In the Make, adding body mass drops nearest neighbour to about 70% (grams swamp the distance) while the tree reaches 97%: a real lesson about scale, which the info question explains.

        Running the lesson

        1. Predict: ask what score on the training data means, from last lesson.
        2. Investigate: do the match as a class, pointing at last lesson's code.
        3. Make: collect the class's best combinations on the board.

        The hard part (on purpose)

        Mapping library lines to their hand-built versions. Ask students to annotate each line with the lesson it came from before running.

        When students get stuck

        Stretch and support

        Stretch: Add StandardScaler (from sklearn.preprocessing) before the neighbours model with make_pipeline, and see the body-mass result recover.

        Support: Give students the DecisionTreeClassifier lines, and ask them only to score it on the test set.

        Where it leads

        Next lesson puts the model behind a Gradio app. The decision tree's rules can be printed with sklearn.tree.export_text.

        What each step is for

        Now you can
        Next lesson5. A model behind an appPut the penguin model behind a Gradio app: sliders for the measurements, and the app says the species and how sure it is.