Start here

Run it

The penguins from Python for AI, one row a penguin. Every number below is the videos' own, since the piles are dealt with the same random_state.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.

1Eighty-six kept aside

Run it

From What machine learning is

Run the cell. It keeps the two features of ml-what-is-ml, the flipper and the bill's length, with the species as the label. Then it seals a quarter of the penguins away before anyone looks. stratify=y keeps each kind's share the same in both piles.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.

2The rule by hand

Your turn

From What machine learning is

Write the video's rule in pandas, on the 86 kept aside. Every penguin starts as an Adelie. A bill over 43 mm makes it a Chinstrap. Then a flipper over 206 mm makes it a Gentoo, whatever its bill.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Hint

Each blank is a comparison on one column of X_test. The flipper's line comes second, so it wins over the bill.

One way to do it
guess = pd.Series("Adelie", index=X_test.index)
guess[X_test["bill_length_mm"] > 43] = "Chinstrap"
guess[X_test["flipper_length_mm"] > 206] = "Gentoo"
hand_share = (guess == y_test).mean()
print(hand_share)

3The rule a tree finds

Your turn

From What machine learning is

Now let the computer find the rule. Make a tree that may ask two questions, fit it on the 256 penguins it may learn from, and print its cuts. Then keep its share right on the 86 in tree_share.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Hint

fit takes the training features and their labels. score takes the pile you judge it on.

One way to do it
tree = DecisionTreeClassifier(max_depth=2, random_state=0)
tree.fit(X_train, y_train)
print(export_text(tree, feature_names=cols))
tree_share = tree.score(X_test, y_test)
print(tree_share)

4Every one right

Your turn

From Training, validation and test data

Let the tree ask as many questions as it likes: max_depth=None. Score it on the rows it learned from, and keep that in own_score. The next line scores it on the 86 it never saw.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Hint

The rows it learned from are the training set.

One way to do it
free = DecisionTreeClassifier(max_depth=None, random_state=0)
free.fit(X_train, y_train)
own_score = free.score(X_train, y_train)
print(own_score, free.score(X_test, y_test))

5Three piles

Your turn

From Training, validation and test data and What machine learning is

To choose how many questions, you need a pile that is not the test set. Deal the same test set as before, then cut the rest again: a quarter of it becomes the validation set, stratified the same way. The loop then tries each depth on it, and the winner opens the test set once.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Hint

The second call is the first one again, with the rest in place of X and y, and stratify=y_rest.

One way to do it
X_rest, X_test, y_rest, y_test = train_test_split(
    X, y, test_size=0.25, stratify=y, random_state=0)
X_train, X_val, y_train, y_val = train_test_split(
    X_rest, y_rest, test_size=0.25, stratify=y_rest, random_state=0)
for depth in [1, 2, 3, 4, 5, None]:
    tree = DecisionTreeClassifier(max_depth=depth, random_state=0)
    tree.fit(X_train, y_train)
    print(depth, tree.score(X_val, y_val))
best = DecisionTreeClassifier(max_depth=2, random_state=0)
best.fit(X_train, y_train)
print("test, opened once:", best.score(X_test, y_test))

6A line for the mass

Your turn

From Linear regression and Training, validation and test data

Now the label is a number: the mass in grams of the 333 penguins fully measured. The cell fits a line from the flipper, and a baseline that always guesses the training set's average. Write scores: the MAE, the RMSE and R² of a guess on the test set.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.

The video's average missed by 708 g, as it was the test set's own. The training set's is the one you could really guess: its R² is just under 0.

Hint

Each score takes the real masses first and the guesses second: mean_absolute_error(y_test, guess), and the same for the other two.

One way to do it
df = penguins.dropna()
X_train, X_test, y_train, y_test = train_test_split(
    df[["flipper_length_mm"]], df["body_mass_g"], test_size=0.25, random_state=0)
model = LinearRegression()
model.fit(X_train, y_train)
guess = model.predict(X_test)
average = np.full(len(y_test), y_train.mean())

def scores(guess):
    return [mean_absolute_error(y_test, guess), root_mean_squared_error(y_test, guess), r2_score(y_test, guess)]

print("line   ", np.round(scores(guess), 2))
print("average", np.round(scores(average), 2))

7Sex and species

On your own

From Linear regression and Training, validation and test data

On your own now. A model reads numbers only, so the sex as words stops it: run the first cell. Then add sex and species to the flipper as columns of zeros and ones with pd.get_dummies(…, drop_first=True), deal the same test set (test_size=0.25, random_state=0), fit a line, and keep its three scores in with_both.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Hint

pd.get_dummies takes the three columns and gives back the flipper as it was, with sex_MALE and a column for two of the species. scores still reads y_test, so deal the new piles before you call it.

One way to do it
X = pd.get_dummies(df[["flipper_length_mm", "sex", "species"]], drop_first=True)
X_train, X_test, y_train, y_test = train_test_split(
    X, df["body_mass_g"], test_size=0.25, random_state=0)
model = LinearRegression()
model.fit(X_train, y_train)
with_both = scores(model.predict(X_test))
print(np.round(with_both, 2))

Run on Pyodide 314, Python 3.14, pandas 3.0 and scikit-learn 1.8. Checked on .