Implement Weighted Binary Log Loss with Clipping in NumPy

Quick Overview

Implement binary log loss in NumPy with probability clipping and optional sample weights, returning the weighted mean as a Python float. Tests numerical stability near 0 and 1, weighted averaging and input validation.

Implement Weighted Binary Log Loss with Clipping in NumPy

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `binary_log_loss(y_true, y_pred_prob, sample_weights=None, eps=1e-15)` using only NumPy. Clip the probabilities to `[eps, 1 - eps]`, compute the per-sample loss `-(y*log(p) + (1-y)*log(1-p))`, and return the weighted mean `sum(w*l) / sum(w)` as a Python `float`. Use uniform weights when `sample_weights` is `None`. Example: ```python y_true = [1, 0, 1, 0] y_pred_prob = [0.9, 0.1, 0.8, 0.3] binary_log_loss(y_true, y_pred_prob) # 0.19763 = -(ln 0.9 + ln 0.9 + ln 0.8 + ln 0.7) / 4 binary_log_loss([1, 0], [0.0, 1.0]) # finite: both probabilities are clipped to the eps bounds ``` ```hint Normalize the inputs first Convert every input to a flat float64 array before doing any arithmetic, so lists, arrays and column vectors all go through the same length and emptiness checks. ``` ### Constraints & Assumptions - Inputs may be Python lists or NumPy arrays. - Labels are 0 or 1, and probabilities lie in `[0, 1]`. - Samples with zero weight contribute nothing to the result. - Raise `ValueError` on mismatched lengths, empty input, or weights that sum to zero. ### What a Strong Answer Covers - Clipping applied before either log, so predictions of exactly 0 or 1 give a large but finite loss. - The weighted mean normalized by the weight sum, with uniform weights as the default. - Each `ValueError` case, including weights whose length does not match the labels. - A plain Python `float` as the return value, computed in one O(n) pass. ### Follow-up Questions - Why clip the probabilities instead of adding `eps` inside the log? - If the model outputs logits, how would you compute the same loss without converting to probabilities first, and why is that more stable? - How would you choose sample weights to correct for class imbalance, and what does the weighted loss estimate then? - What should the function do with negative weights or labels outside {0, 1}?

Overview: Implement binary log loss in NumPy with probability clipping and optional sample weights, returning the weighted mean as a Python float. Tests numerical stability near 0 and 1, weighted averaging and input validation.

General logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement binary_log_loss(y_true, y_pred_prob, sample_weights=None, eps=1e-15) using only NumPy. Clip the probabilities to [eps, 1 - eps], compute the per-sample loss -(y*log(p) + (1-y)*log(1-p)), and return the weighted mean sum(w*l) / sum(w) as a Python float. Use uniform weights when sample_weights is None.

Example:

y_true = [1, 0, 1, 0]
y_pred_prob = [0.9, 0.1, 0.8, 0.3]
binary_log_loss(y_true, y_pred_prob)  # 0.19763 = -(ln 0.9 + ln 0.9 + ln 0.8 + ln 0.7) / 4

binary_log_loss([1, 0], [0.0, 1.0])  # finite: both probabilities are clipped to the eps bounds

Constraints & Assumptions

  • Inputs may be Python lists or NumPy arrays.
  • Labels are 0 or 1, and probabilities lie in [0, 1] .
  • Samples with zero weight contribute nothing to the result.
  • Raise ValueError on mismatched lengths, empty input, or weights that sum to zero.

What a Strong Answer Covers Guidance

  • Clipping applied before either log, so predictions of exactly 0 or 1 give a large but finite loss.
  • The weighted mean normalized by the weight sum, with uniform weights as the default.
  • Each ValueError case, including weights whose length does not match the labels.
  • A plain Python float as the return value, computed in one O(n) pass.

Follow-up Questions Guidance

  • Why clip the probabilities instead of adding eps inside the log?
  • If the model outputs logits, how would you compute the same loss without converting to probabilities first, and why is that more stable?
  • How would you choose sample weights to correct for class imbalance, and what does the weighted loss estimate then?
  • What should the function do with negative weights or labels outside {0, 1}?