Find the best constant-prediction log-loss baseline for binary labels
Quick Overview
Find the constant probability that minimizes binary log loss and the loss it achieves, solved in closed form. It tests calculus on the cross-entropy objective, the link to binary entropy and intercept-only logistic regression, and how to use the result as a baseline for a trained classifier.
Find the best constant-prediction log-loss baseline for binary labels
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Given a 1-D array `y_true` of 0/1 labels of shape `(n,)` containing both classes, implement `best_constant_log_loss(y_true)`. Predicting a single probability $c$ for every sample costs
$$L(c) = -\frac{1}{n} \sum_{i=1}^{n} \left[ y_i \log c + (1 - y_i) \log (1 - c) \right]$$
with the natural log. Return `(best_c, best_loss)`, where `best_c` minimizes $L$ over $c$ in $(0, 1)$ and `best_loss = L(best_c)`.
Requirements:
- Solve analytically, not by search: $L$ is minimized at the base rate `c* = mean(y_true)`.
- The minimum loss is the binary entropy `-(c*log(c*) + (1-c*)log(1-c*))`.
- Return a 2-tuple of Python `float` values.
Example:
```python
y_true = np.array([1,0,1,1,0,1,0,0,1,1])
best_constant_log_loss(y_true)
# -> (0.6, 0.6730116670092565)
```
```hint Where the minimum is
$L$ is a smooth function of one variable. Write the sum in terms of the number of positives, differentiate, and set the derivative to zero.
```
### Constraints & Assumptions
- `y_true` is a 1-D array of 0/1 labels with at least one 0 and one 1, so the base rate is strictly between 0 and 1.
- Logs are natural logs, so the loss is in nats.
### What a Strong Answer Covers
- Derives the minimizer from $L'(c) = 0$ and shows it is the unique minimum.
- Substitutes back to get the binary entropy of the base rate.
- Returns Python floats from a single $O(n)$ pass with no numeric search.
- Explains what the number means as a baseline for a trained classifier, on training data and on held-out data.
- Handles the single-class case, where the formula hits `log(0)`.
### Follow-up Questions
- Why does an intercept-only logistic model converge to `mean(y_true)`?
- How would you guard against `log(0)` if a class were absent?
- A trained model scores a log loss of 0.66 on a test set whose base rate is 0.6. What does that tell you?
- How would you turn this baseline into a normalized score for comparing models across datasets with different base rates?
Overview: Find the constant probability that minimizes binary log loss and the loss it achieves, solved in closed form. It tests calculus on the cross-entropy objective, the link to binary entropy and intercept-only logistic regression, and how to use the result as a baseline for a trained classifier.
Find the best constant-prediction log-loss baseline for binary labels
General
Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0
Given a 1-D array y_true of 0/1 labels of shape (n,) containing both classes, implement best_constant_log_loss(y_true). Predicting a single probability c for every sample costs
L(c)=−n1∑i=1n[yilogc+(1−yi)log(1−c)]
with the natural log. Return (best_c, best_loss), where best_c minimizes L over c in (0,1) and best_loss = L(best_c).
Requirements:
Solve analytically, not by search:
L
is minimized at the base rate
c* = mean(y_true)
.
The minimum loss is the binary entropy
-(c*log(c*) + (1-c*)log(1-c*))
.