All problems
Class-Weighted Log Loss
Problem
Compute binary cross-entropy with class weights: each sample is weighted by w1 if y = 1 and w0 if y = 0, and the total is divided by the sum of weights.
Probabilities are clipped to [1e-15, 1 - 1e-15]. Empty input or zero total weight returns 0.0.
Hint
Upweighting a class makes errors on that class cost more in the loss.
Test cases
case 1
in: [[1,0],[0.9,0.1],2,1]
expected: 0.10536051565782628
case 2
in: [[1,1,0,0],[0.5,0.5,0.5,0.5],1,1]
expected: 0.6931471805599453
case 3
in: [[0,0],[0.2,0.8],1,3]
expected: 0.9162907318741551
case 4
in: [[],[],1,1]
expected: 0
Starter codePython
import math
def weighted_log_loss(y_true, y_pred, w0, w1):
# Your code here
pass4 test cases · run your code against them in the editor