Back to Practice
#0014
Binary Cross Entropy Loss
MediumML20 min15 XP
Problem
Given binary labels and predicted probabilities, return the mean binary cross entropy. Clip probabilities to avoid log(0).
Why This Matters
Binary cross entropy is the loss behind logistic regression and many binary classifiers. It punishes confident wrong predictions heavily.
Function Signature
def binary_cross_entropy(y_true, y_prob):
Examples
Example 1
Inputy_true = [1, 0], y_prob = [0.9, 0.2]
Outputabout 0.1643
Both predictions are fairly confident and correct, so the average loss is low.
Constraints
- y_true contains 0 and 1 labels.
- y_prob contains probabilities between 0 and 1.
- Return the mean loss as a float.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 4 hidden categories
Mostly correct probabilities
Input[1, 0], [0.9, 0.2]
Expectedapproximately 0.1643
Correct confident predictions produce small loss.
Clips extreme probabilities
Input[1, 0], [1.0, 0.0]
Expectedfinite loss
Clipping avoids log(0).
Hidden Test Categories
Confident wrong predictionsAll positive labelsAll negative labelsLonger arrays