Show code
import pandas as pd
from plotnine import (
aes,
geom_abline,
geom_point,
ggplot,
labs,
theme_bw,
)
from plotnine.data import penguins as penguins_raw
from sklearn.ensemble import (
GradientBoostingRegressor,
RandomForestClassifier,
RandomForestRegressor,
)
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
RANDOM_STATE = 123
# Load and clean the data once for the entire presentation.
penguins = penguins_raw.dropna().copy()
regression_features = [
"bill_depth_mm",
"bill_length_mm",
"flipper_length_mm",
]
classification_features = [
"body_mass_g",
"bill_depth_mm",
"bill_length_mm",
"flipper_length_mm",
]
# Create one reproducible 50/50 split and reuse it in every example.
train_index, test_index = train_test_split(
penguins.index,
test_size=0.5,
random_state=RANDOM_STATE,
)
train_data = penguins.loc[train_index].copy()
test_data = penguins.loc[test_index].copy()
X_train_reg = train_data[regression_features]
y_train_reg = train_data["body_mass_g"]
X_test_reg = test_data[regression_features]
y_test_reg = test_data["body_mass_g"]
X_train_class = train_data[classification_features]
y_train_class = train_data["species"]
X_test_class = test_data[classification_features]
y_test_class = test_data["species"]

