Answer key · 12 questions

Python model evaluation and accuracy Exercises With Answers

All 12 questions for Python model evaluation and accuracy in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python model evaluation and accuracy, with the exact answer and a short explanation for every question.

The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.

01 · Multiple choice

Why keep a separate test set?

  1. To make training slower
  2. To estimate performance on unseen examplescorrect
  3. To increase the number of features
  4. To replace the need for labels

Answer: To estimate performance on unseen examples

Why: A held-out set gives a more honest estimate than scoring the training data again.

02 · Multiple choice

What is the denominator of precision?

  1. True negatives
  2. All examples
  3. Predicted positivescorrect
  4. Actual positives

Answer: Predicted positives

Why: Precision is true positives divided by true positives plus false positives.

03 · Multiple choice

What is the denominator of recall?

  1. True positives only
  2. All negatives
  3. Predicted positives
  4. Actual positivescorrect

Answer: Actual positives

Why: Recall is true positives divided by true positives plus false negatives.

04 · Multiple choice

What is the output?

actual = [1, 1, 0, 0]
predicted = [1, 0, 0, 1]
print(sum(a == 1 and p == 1 for a, p in zip(actual, predicted)))
  1. 3
  2. 1correct
  3. 4
  4. 2

Answer: 1

Why: Only the first pair has actual 1 and predicted 1.

05 · Fill in the blank

Complete the true-positive condition.

actual = [1, 1, 0, 0]
predicted = [1, 0, 0, 1]
tp = sum(a == 1 and p == ____ for a, p in zip(actual, predicted))
print(tp)

Answer: 1

Why: A true positive requires the actual label and the prediction to both be positive.

06 · Fill in the blank

Complete the precision denominator.

tp, fp, fn = 8, 2, 4
precision = tp / (tp + ____)
print(round(precision, 2))

Answer: fp

Why: Precision divides by all predicted positives: true positives plus false positives.

07 · Fill in the blank

Complete the recall denominator.

tp, fp, fn = 8, 2, 4
recall = tp / (tp + ____)
print(round(recall, 2))

Answer: fn

Why: Recall divides by all actual positives: true positives plus false negatives.

08 · Fill in the blank

Complete the accuracy denominator.

actual = [1, 1, 0, 0]
predicted = [1, 0, 0, 1]
accuracy = sum(a == p for a, p in zip(actual, predicted)) / ____(actual)
print(round(accuracy, 2))

Answer: len

Why: Accuracy divides the number of correct predictions by the number of examples.

09 · Predict output

What does this code print?

actual = [1, 1, 0, 0]
predicted = [1, 0, 0, 1]
tp = sum(a == 1 and p == 1 for a, p in zip(actual, predicted))
tn = sum(a == 0 and p == 0 for a, p in zip(actual, predicted))
fp = sum(a == 0 and p == 1 for a, p in zip(actual, predicted))
fn = sum(a == 1 and p == 0 for a, p in zip(actual, predicted))
print(tp, tn, fp, fn)

Answer: 1 1 1 1

Why: Each of the four confusion-matrix cells contains one example.

10 · Predict output

What does the program output?

tp, fp, fn = 8, 2, 4
precision = tp / (tp + fp)
print(round(precision, 2))

Answer: 0.8

Why: Precision is 8 divided by 10, which rounds to 0.8.

11 · Predict output

What appears on the screen?

tp, fp, fn = 8, 2, 4
recall = tp / (tp + fn)
print(round(recall, 2))

Answer: 0.67

Why: Recall is 8 divided by 12, which rounds to 0.67.

12 · Predict output

What is printed?

actual = [0] * 95 + [1] * 5
predicted = [0] * 100
accuracy = sum(a == p for a, p in zip(actual, predicted)) / len(actual)
print(round(accuracy, 2))

Answer: 0.95

Why: Predicting all negative is 95% accurate but finds none of the five positives.

Try the questions first

Open the practice page for Python model evaluation and accuracy and answer the same 12 questions with instant feedback.

Practice this lesson