Loading content...
In machine learning, evaluating the performance of binary classification models requires a robust loss function that measures the discrepancy between predicted probabilities and actual class labels. The Binary Classification Loss (also known as Log Loss or Logarithmic Loss) is the standard metric used for this purpose, providing a smooth, differentiable objective that heavily penalizes confident but incorrect predictions.
Given a list of true binary labels y_true (where each element is either 0 or 1) and a corresponding list of predicted probabilities y_pred (where each element is a value between 0 and 1 representing the model's confidence that the sample belongs to class 1), compute the mean binary classification loss across all samples.
Mathematical Formulation:
For a single sample with true label y and predicted probability p, the binary classification loss is computed as:
$$L(y, p) = -[y \cdot \log(p) + (1 - y) \cdot \log(1 - p)]$$
For a dataset with N samples, the mean loss is:
$$\text{Mean Loss} = \frac{1}{N} \sum_{i=1}^{N} L(y_i, p_i)$$
Numerical Stability:
Taking the logarithm of values exactly equal to 0 or 1 results in undefined or infinite values. To ensure numerical stability, clip the predicted probabilities to the range [epsilon, 1 - epsilon] before computing the logarithm. The default value for epsilon is 1e-15.
Your Task:
Write a Python function that:
Behavior:
y_true = [1, 0, 1, 0]
y_pred = [0.9, 0.1, 0.8, 0.2]0.1643For each sample, we compute the individual loss L(y, p) = -[y × log(p) + (1 - y) × log(1 - p)]:
• Sample 0 (y=1, p=0.9): L = -(1 × log(0.9) + 0 × log(0.1)) = -log(0.9) ≈ 0.1054 • Sample 1 (y=0, p=0.1): L = -(0 × log(0.1) + 1 × log(0.9)) = -log(0.9) ≈ 0.1054 • Sample 2 (y=1, p=0.8): L = -(1 × log(0.8) + 0 × log(0.2)) = -log(0.8) ≈ 0.2231 • Sample 3 (y=0, p=0.2): L = -(0 × log(0.2) + 1 × log(0.8)) = -log(0.8) ≈ 0.2231
Mean Loss = (0.1054 + 0.1054 + 0.2231 + 0.2231) / 4 ≈ 0.1643
y_true = [1, 1, 0, 0]
y_pred = [0.7, 0.8, 0.3, 0.2]0.2899For each sample, we compute the individual loss:
• Sample 0 (y=1, p=0.7): L = -log(0.7) ≈ 0.3567 • Sample 1 (y=1, p=0.8): L = -log(0.8) ≈ 0.2231 • Sample 2 (y=0, p=0.3): L = -log(0.7) ≈ 0.3567 • Sample 3 (y=0, p=0.2): L = -log(0.8) ≈ 0.2231
Mean Loss = (0.3567 + 0.2231 + 0.3567 + 0.2231) / 4 ≈ 0.2899
y_true = [1, 0, 1, 0, 1]
y_pred = [0.95, 0.05, 0.75, 0.25, 0.6]0.2378For each sample, we compute the individual loss:
• Sample 0 (y=1, p=0.95): L = -log(0.95) ≈ 0.0513 • Sample 1 (y=0, p=0.05): L = -log(0.95) ≈ 0.0513 • Sample 2 (y=1, p=0.75): L = -log(0.75) ≈ 0.2877 • Sample 3 (y=0, p=0.25): L = -log(0.75) ≈ 0.2877 • Sample 4 (y=1, p=0.6): L = -log(0.6) ≈ 0.5108
Mean Loss = (0.0513 + 0.0513 + 0.2877 + 0.2877 + 0.5108) / 5 ≈ 0.2378
Constraints