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.
Why keep a separate test set?
Answer: To estimate performance on unseen examples
Why: A held-out set gives a more honest estimate than scoring the training data again.
What is the denominator of precision?
Answer: Predicted positives
Why: Precision is true positives divided by true positives plus false positives.
What is the denominator of recall?
Answer: Actual positives
Why: Recall is true positives divided by true positives plus false negatives.
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)))Answer: 1
Why: Only the first pair has actual 1 and predicted 1.
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.
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.
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.
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.
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.
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.
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.
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.