Class-Weighted Log Loss From Scratch

Quick Overview

Implement a class-weighted binary log loss from scratch in NumPy, where each sample takes the weight of its true label and the total is normalized by the sum of weights. It checks vectorized loss code, weighted averaging and how class weights help with imbalanced labels.

Class-Weighted Log Loss From Scratch

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `class_weighted_log_loss(y_true, y_pred, class_weight)` from scratch with `numpy` (no library loss functions). Each sample gets weight `w_i = class_weight[y_true[i]]`. Return `sum(w_i * loss_i) / sum(w_i)` as a Python `float`, where `loss_i = -(y_i * log(p_i) + (1 - y_i) * log(1 - p_i))`. For example: ```python y_true = [1, 0, 1, 1, 0] y_pred = [0.9, 0.1, 0.8, 0.6, 0.3] class_weight = {0: 2.0, 1: 0.5} class_weighted_log_loss(y_true, y_pred, class_weight) # -> 0.24431559355675106 ``` ```hint Two arrays, one reduction Build the per-sample loss and the per-sample weight as two arrays of length n. Once you have both, the answer is a single expression. Check which quantity goes in the denominator. ``` ### Constraints & Assumptions - `y_true` has shape `(n,)` and holds 0/1 labels. - `y_pred` has shape `(n,)` and holds P(y=1), strictly inside `(0, 1)`, so no clipping is needed. - `class_weight` maps labels `0` and `1` to positive float weights. - The denominator is the sum of sample weights, not `n`. ### What a Strong Answer Covers - A vectorized per-sample binary cross-entropy that uses no library loss function - A weight vector built from the true labels with a vectorized lookup - Normalizing by the sum of weights and returning a Python `float` - Input checks that matter (matching shapes, labels outside 0/1, empty input) and why the stated range of `y_pred` removes the need for clipping - Time and space complexity ### Follow-up Questions - How does class weighting differ from per-sample `sample_weight`? - How would you set the weights to counter class imbalance? - What would you change if `y_pred` could contain exactly 0 or 1?

Overview: Implement a class-weighted binary log loss from scratch in NumPy, where each sample takes the weight of its true label and the total is normalized by the sum of weights. It checks vectorized loss code, weighted averaging and how class weights help with imbalanced labels.

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

Implement class_weighted_log_loss(y_true, y_pred, class_weight) from scratch with numpy (no library loss functions). Each sample gets weight w_i = class_weight[y_true[i]]. Return sum(w_i * loss_i) / sum(w_i) as a Python float, where loss_i = -(y_i * log(p_i) + (1 - y_i) * log(1 - p_i)).

For example:

y_true = [1, 0, 1, 1, 0]
y_pred = [0.9, 0.1, 0.8, 0.6, 0.3]
class_weight = {0: 2.0, 1: 0.5}
class_weighted_log_loss(y_true, y_pred, class_weight)
# -> 0.24431559355675106

Constraints & Assumptions

  • y_true has shape (n,) and holds 0/1 labels.
  • y_pred has shape (n,) and holds P(y=1), strictly inside (0, 1) , so no clipping is needed.
  • class_weight maps labels 0 and 1 to positive float weights.
  • The denominator is the sum of sample weights, not n .

What a Strong Answer Covers Guidance

  • A vectorized per-sample binary cross-entropy that uses no library loss function
  • A weight vector built from the true labels with a vectorized lookup
  • Normalizing by the sum of weights and returning a Python float
  • Input checks that matter (matching shapes, labels outside 0/1, empty input) and why the stated range of y_pred removes the need for clipping
  • Time and space complexity

Follow-up Questions Guidance

  • How does class weighting differ from per-sample sample_weight ?
  • How would you set the weights to counter class imbalance?
  • What would you change if y_pred could contain exactly 0 or 1?