DIP+CV · Programming Exercise

EP01_02 — 📊 Predictive Performance — ML Metrics in CV

1.18.5 EP01_02 📊 Predictive Performance — ML Metrics in CV

In this activity, you will dive into the world of Machine Learning. Your goal is to evaluate the performance of a binary classifier by calculating metrics from a Confusion Matrix.

1.18.5.1 🧠 Why does the right metric matter?

Imagine a R$ 1.00 coin detector. The impact of the error defines the priority metric:

Metric Practical Example Importance in IP/CV
Accuracy Grain Counting Useful when classes are balanced (e.g., half of the grains defective, half healthy).
Precision Security/Biometrics Crucial to avoid False Positives (e.g., not allowing an impostor to access a system due to recognition errors).
Sensitivity Health (Tumors) Crucial to avoid False Negatives (e.g., not letting a tumor go unnoticed in an X-ray examination).
F1-score Banknotes Ideal for a balance between not rejecting genuine notes and not accepting counterfeit ones.

1.18.5.2 📊 The Confusion Matrix

Predicted Positive Predicted Negative
Actual Positive TP (True Positive) FN (False Negative)
Actual Negative FP (False Positive) TN (True Negative)

Task:

  1. Read 4 integer values in the order: TP, FN, FP, TN.
  2. Calculate the metrics using the formulas:

\[\text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN}\]

\[\text{Precision} = \frac{TP}{TP + FP}\]

\[\text{Sensitivity (Recall)} = \frac{TP}{TP + FN}\]

\[\text{F1-score} = \frac{2 \times \text{Precision} \times \text{Sensitivity}}{\text{Precision} + \text{Sensitivity}}\]

  1. Print the results formatted with two decimal places, one per line.

📌 Important:

  • Use floating-point division to avoid truncated results.
  • The output order must be: Accuracy, Precision, Sensitivity, and F1-score.
  • See the interactive simulation for this EP at Figure 1.12.

1.18.5.3 📌 Example Execution

Input Output Observation
40 0.75 Accuracy
10 0.73 Precision
15 0.80 Sensitivity
35 0.76 F1-score

(Note: TP=40, FN=10, FP=15, TN=35. Total cases = 100)

🎮 Simulator EP01_02: Predictive Performance — ML Metrics Confusion Matrix & Metrics

Select a predefined scenario or adjust the sliders to observe the real-time impact on the confusion matrix and evaluation metrics.

INPUTS (MATRIX PARAMETERS)
40
10
15
35
DISCRETE CONFUSION MATRIX
Pred +
Pred −
Actual +
TP
40
FN
10
Actual −
FP
15
TN
35
REAL-TIME CALCULATED METRICS
Accuracy 0.75
(TP + TN) / Total
Precision 0.73
TP / (TP + FP)
Sensitivity (Recall) 0.80
TP / (TP + FN)
F1-Score (Harmonic Mean) 0.76
2 · (P · R) / (P + R)
Total Samples: 100
Figure 1.12: EP01_02 Simulator: Predictive Performance — ML Metrics
# your solution
TestSuite("EP01_02.py").run()
✔️ EP01_02.cases already exists in casos/
📋 5 case(s) loaded from casos/EP01_02.cases
💥 File EP01_02.py not found.