Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

136. Vision model evaluation metrics

easy
GeneralGeneral
senior

Evaluate a computer vision model using common metrics so you can compare predictions against ground-truth labels.

You’ll implement accuracy, precision, recall, and F1-score for a binary image classifier given lists of predicted labels and true labels.

Requirements

Implement the function

python

Rules:

  • Use the confusion-matrix counts: TP, TN, FP, FN.
  • Compute metrics using: Accuracy=TP+TNTP+TN+FP+FN,Precision=TPTP+FP,Recall=TPTP+FN,F1=2â‹…Precisionâ‹…RecallPrecision+Recall\text{Accuracy}=\frac{TP+TN}{TP+TN+FP+FN},\quad \text{Precision}=\frac{TP}{TP+FP},\quad \text{Recall}=\frac{TP}{TP+FN},\quad F1=\frac{2\cdot \text{Precision}\cdot \text{Recall}}{\text{Precision}+\text{Recall}}
  • Return a Python dict with the four metrics.
  • Don’t call any prebuilt metric functions (no sklearn.metrics).
  • Key considerations (keep it simple):
    • Compute TP/TN/FP/FN via boolean masks and sums.
    • Return floats (not NumPy scalar types).
    • Keep everything inside this single function.
    • Assume denominators are non-zero (no need for special-case handling).

Example

python

Output:

python
Input Signature
ArgumentType
y_prednp.ndarray
y_truenp.ndarray
Output Signature
Return NameType
valuedict

Constraints

  • No sklearn.metrics or prebuilt metric functions

  • Return Python float values, not NumPy scalars

Hint 1

Inputs are NumPy arrays, so you can compare all elements at once.

Hint 2

Compute confusion-matrix counts with boolean masks, e.g. tp = np.sum((y_true==1) & (y_pred==1)), similarly for TN/FP/FN.

Hint 3

Plug TP/TN/FP/FN into the given formulas, then return a dict with Python floats (wrap results with float(...)).

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
senior
entry
Tags
binary-classification
confusion-matrix
numpy-vectorization
evaluation-metrics
42 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit