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.
Make your prediction first. Help opens once you've locked it in.
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?
Run it (wait for scikit-learn to load). Then change the mystery penguin's measurements and run again.
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])
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.
This says the model is 100% accurate. Click the line that is wrong.
Let the 5 nearest penguins vote instead of 1. Better, worse or the same?
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.
from sklearn.tree import DecisionTreeClassifier, then tree = DecisionTreeClassifier(max_depth=3, random_state=1).
fit it on the same training data and score it on the same test data.
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.
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 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.
Next: put the model behind a Gradio app, so anyone can use it.
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.
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.
Mapping library lines to their hand-built versions. Ask students to annotate each line with the lesson it came from before running.
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.
Next lesson puts the model behind a Gradio app. The decision tree's rules can be printed with sklearn.tree.export_text.