Topics / 🤖 AI & Machine Learning / 📏 Level 22: Evaluate & Engineer

Python exercise · about 5 minutes · free

Precision and Recall by Hand

Accuracy hides what matters. Did we catch the defaulters (recall)? Were our alarms right (precision)?

Count TP (predicted 1, really 1), FP (predicted 1, really 0), FN (predicted 0, really 1) and TN. Precision = TP / (TP + FP). Recall = TP / (TP + FN).

Table. confusion matrix: , predicted 1, predicted 0; actual 1 | TP 3 | FN 1; actual 0 | FP 2 | TN 4. precision = 3 / (3+2) = 0.6 · recall = 3 / (3+1) = 0.75confusion matrixpredicted 1predicted 0actual 1TP 3FN 1actual 0FP 2TN 4precision = 3 / (3+2) = 0.6 · recall = 3 / (3+1) = 0.75
Your quest: Set tp, fp, fn, tn, precision and recall.
Try it now Runs in your browser. No sign-in needed. Ctrl+Enter to run.
Hintpairs = list(zip(y_true, y_pred)); tp = pairs.count((1, 1)); fp = pairs.count((0, 1)); fn = pairs.count((1, 0)); tn = pairs.count((0, 0))
Output appears here.

Open it in the full course → Free with Google: saves your progress and unlocks 627 lessons.

In this level you'll learn:
More in Level 22: Evaluate & Engineer

Share on LinkedIn