Chapters
Python114 chapters

Exercises

Evaluating a Model

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

Exercise 1

This model never predicts the rare class. Print its accuracy and its recall on that class.

Python needs scikit-learn, downloaded on first run
import numpy as np
from sklearn.metrics import accuracy_score, recall_score

actual = np.array([0] * 99 + [1])
predicted = np.zeros(100, dtype=int)
# print accuracy, then recall on class 1
Exercise 2

Print the confusion matrix for the model's predictions.

Python needs scikit-learn, downloaded on first run
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(
    data.data, data.target, test_size=0.2, random_state=0, stratify=data.target
)
model = LogisticRegression(max_iter=5000).fit(X_train, y_train)
# print the confusion matrix
Exercise 3

Print the full classification report, with the species names rather than 0, 1, 2.

Python needs scikit-learn, downloaded on first run
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier

data = load_iris()
X_train, X_test, y_train, y_test = train_test_split(
    data.data, data.target, test_size=0.3, random_state=0, stratify=data.target
)
model = DecisionTreeClassifier(random_state=0).fit(X_train, y_train)
# print the report