Back to Practice
#0020

Binary Cross Entropy From Probabilities

MediumML18 min15 XP

Problem

Given true labels y_true and predicted probabilities y_prob, return average binary cross entropy. Clip probabilities to avoid log(0).

Why This Matters

Binary cross entropy is the loss behind many churn, fraud, medical-risk, and click prediction models.

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.164

Confident correct probabilities produce a low loss.

Constraints

  • y_true contains 0 and 1 labels.
  • y_prob contains predicted probabilities.
  • Return the mean loss as a float.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 4 hidden categories
Confident mostly correct predictions
Input[1, 0], [0.9, 0.2]
Expectedabout 0.164

Both predictions lean toward the correct class.

Avoid log zero
Input[1, 0], [1.0, 0.0]
Expectedfinite loss near zero

Clipping prevents log(0) from producing infinity.

Hidden Test Categories
All positive labelsAll negative labelsBad confident predictionsProbabilities close to 0 or 1