Sample-Weighted Log Loss From Scratch

Quick Overview

Implement sample-weighted binary log loss in NumPy without calling scikit-learn, and match log_loss when sample_weight is passed. Tests the cross-entropy formula, weighted averaging with the right denominator and careful handling of predictions near 0 or 1.

Sample-Weighted Log Loss From Scratch

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `weighted_log_loss(y_true, y_pred, sample_weight)` from scratch, without calling `sklearn.metrics.log_loss`. It returns the sample-weighted mean binary cross-entropy as a Python `float` and must match `sklearn.metrics.log_loss(y_true, y_pred, sample_weight=sample_weight)`. Requirements: - The per-sample loss uses the natural log: `loss_i = -(y_i*ln(p_i) + (1-y_i)*ln(1-p_i))`. - Return `sum(w_i * loss_i) / sum(w_i)`. Divide by `sum(w_i)`, not by `n`. Example: ```python y_true = [1, 0, 1, 1, 0] y_pred = [0.9, 0.1, 0.8, 0.6, 0.3] sample_weight = [1, 2, 3, 1, 1] weighted_log_loss(y_true, y_pred, sample_weight) # 0.23162659607760389 ``` ```hint Test the denominator Run your function with every weight set to 2. A weighted mean should give the same value as the unweighted mean, so any change points at the denominator. ``` ### Constraints & Assumptions - All inputs are 1-D arrays of shape `(n,)`. The example writes them as lists for brevity. - `y_pred` holds $P(y=1)$ strictly inside $(0, 1)$, so no clipping is needed. - `sample_weight` holds positive values that do not, in general, sum to `n`. ### What a Strong Answer Covers - The per-sample binary cross-entropy with the natural log, vectorized over the arrays - The weighted numerator and the total weight as the denominator, with a reason dividing by `n` gives the wrong value - Matching the `sample_weight` behavior of `sklearn.metrics.log_loss` and returning a plain Python `float` - What happens with predictions at exactly 0 or 1, or with zero weights - Time and space complexity ### Follow-up Questions - How would you make this numerically stable if `y_pred` could equal exactly 0 or 1? - How would you extend this to multiclass log loss? - How does giving a sample a weight of 3 compare with repeating that row three times in the dataset?

Overview: Implement sample-weighted binary log loss in NumPy without calling scikit-learn, and match log_loss when sample_weight is passed. Tests the cross-entropy formula, weighted averaging with the right denominator and careful handling of predictions near 0 or 1.

General logo
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0

Implement weighted_log_loss(y_true, y_pred, sample_weight) from scratch, without calling sklearn.metrics.log_loss. It returns the sample-weighted mean binary cross-entropy as a Python float and must match sklearn.metrics.log_loss(y_true, y_pred, sample_weight=sample_weight).

Requirements:

  • The per-sample loss uses the natural log: loss_i = -(y_i*ln(p_i) + (1-y_i)*ln(1-p_i)) .
  • Return sum(w_i * loss_i) / sum(w_i) . Divide by sum(w_i) , not by n .

Example:

y_true = [1, 0, 1, 1, 0]
y_pred = [0.9, 0.1, 0.8, 0.6, 0.3]
sample_weight = [1, 2, 3, 1, 1]
weighted_log_loss(y_true, y_pred, sample_weight)  # 0.23162659607760389

Constraints & Assumptions

  • All inputs are 1-D arrays of shape (n,) . The example writes them as lists for brevity.
  • y_pred holds P(y=1)P(y=1) strictly inside (0,1)(0, 1) , so no clipping is needed.
  • sample_weight holds positive values that do not, in general, sum to n .

What a Strong Answer Covers Guidance

  • The per-sample binary cross-entropy with the natural log, vectorized over the arrays
  • The weighted numerator and the total weight as the denominator, with a reason dividing by n gives the wrong value
  • Matching the sample_weight behavior of sklearn.metrics.log_loss and returning a plain Python float
  • What happens with predictions at exactly 0 or 1, or with zero weights
  • Time and space complexity

Follow-up Questions Guidance

  • How would you make this numerically stable if y_pred could equal exactly 0 or 1?
  • How would you extend this to multiclass log loss?
  • How does giving a sample a weight of 3 compare with repeating that row three times in the dataset?