Exercises
Evaluating a Model
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
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
recall_score takes pos_label.
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_score(actual, predicted))
print(recall_score(actual, predicted, pos_label=1))Exercise 2Passed
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
confusion_matrix(actual, predicted) from sklearn.metrics.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
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(confusion_matrix(y_test, model.predict(X_test)))Exercise 3Passed
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
classification_report takes target_names. Convert them with str() first.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import classification_report
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)
names = [str(n) for n in data.target_names]
print(classification_report(y_test, model.predict(X_test), target_names=names))