Chapters
Python114 chapters

Exercises

Where It Goes Wrong

3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.

Exercise 1

The rare class is never found. Make the model weight it properly and print its recall.

Python needs scikit-learn, downloaded on first run
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import recall_score

rng = np.random.default_rng(0)
X = rng.normal(size=(1000, 5))
y = (rng.random(1000) < 0.02).astype(int)
X[y == 1] += 0.8

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=0, stratify=y
)
model = LogisticRegression(max_iter=1000).fit(X_train, y_train)
print(round(recall_score(y_test, model.predict(X_test)), 2))
Exercise 2

The leaking column tells the model the answer. Remove it and print the honest score.

Python needs scikit-learn, downloaded on first run
import numpy as np
import pandas as pd
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier

rng = np.random.default_rng(0)
n = 300
y = rng.integers(0, 2, n)
df = pd.DataFrame({
    "age": rng.integers(20, 70, n),
    "visits": rng.integers(1, 10, n),
    "has_cancel_date": y,
})

scores = cross_val_score(DecisionTreeClassifier(random_state=0), df, y, cv=5)
print(round(scores.mean(), 2))
Exercise 3

Compare the model against always predicting the majority class, and print both scores.

Python needs scikit-learn, downloaded on first run
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LogisticRegression

rng = np.random.default_rng(0)
X = rng.normal(size=(500, 4))
y = (rng.random(500) < 0.1).astype(int)

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=0, stratify=y
)
# print the dummy score, then the logistic regression score