Evaluation Metrics
📏 What are evaluation metrics? A metric turns a model's predictions and the true labels into one number you can compare across models. Threshold metrics (precision, recall, F1) score hard yes/no decisions; ranking metrics (ROC AUC, average precision, NDCG) score how well the model orders items; log loss scores the probabilities themselves.
Writing ROC AUC from scratch is the classic version of this question in ML coding rounds, often with precision and recall as a warm-up and NDCG for ranking teams; the details that separate answers are ties, empty classes and logarithms of zero.
The Prompt
Every metric here is a few lines, so the interviewer listens for definitions you can defend and edge cases you raise before being asked.
"Given binary labels and model scores, compute ROC AUC without a library. Then precision, recall and F1 at a threshold."
Clarifying Questions
You: Labels are 0/1 and scores are any real numbers, higher meaning more likely positive? Both (n,) arrays?
Interviewer: Yes.
You: How should tied scores count? I'd count a positive-negative tie as half a correct pair, which is the standard definition.
Interviewer: Yes, and the answer must not depend on the input order.
You: What if every label is the same class? AUC is undefined then.
Interviewer: Raise an error.
You: And when precision has no predicted positives, 0 / 0?
Interviewer: Return 0 for that metric.
What You'll Build
precision_recall_f1(y_true, y_pred) -> (p, r, f1): at a fixed threshold, with zero-division handled.roc_curve(y, s) -> (fpr, tpr, thresholds)androc_auc(y, s) -> float: exact under ties,O(n log n).average_precision(y, s) -> float: the area under the precision-recall curve, step-wise.ndcg_at_k(rel, scores, k) -> float: graded relevance for ranking.log_loss(y, p)andlog_loss_from_logits(y, z): binary cross-entropy that never returnsinf.