Compute Binary Log-Loss From Two-Column predict_proba Output
Quick Overview
Compute binary log-loss in NumPy from the two-column probability matrix that a logistic regression's predict_proba returns. It tests vectorized indexing by label, the link to the binary cross-entropy formula, and handling of zero probabilities.
Compute Binary Log-Loss From Two-Column predict_proba Output
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `log_loss_two_column(y_true, proba)` to compute binary log-loss from the two-column matrix returned by `LogisticRegression.predict_proba`. For each sample, take `-log` (natural log) of the probability its row assigns to the true class, and return the mean as a Python `float`. Use column 1 when the label is 1 and column 0 when the label is 0.
For example:
```python
y_true = np.array([0, 0, 1, 1])
proba = np.array([[.9, .1], [.8, .2], [.3, .7], [.01, .99]])
log_loss_two_column(y_true, proba) # -> 0.1738073366910675
```
```hint Picking one entry per row
Look for a NumPy indexing pattern that reads a different column in each row, without a Python loop.
```
### Constraints & Assumptions
- `y_true` is a 1-D array of 0/1 labels with shape `(n,)` and contains both classes.
- `proba` has shape `(n, 2)`. Column 0 is `P(y=0)`, column 1 is `P(y=1)`, and each row sums to 1.
- Use the natural log.
### What a Strong Answer Covers
- Selecting the true-class probability for every row in a vectorized way.
- How this column lookup relates to the usual binary cross-entropy formula written with $y$ and ${1 - y}$.
- Returning a Python `float` and confirming the result on the example.
- What happens when a true-class probability is 0, and what dtype the labels need for indexing.
- Time and space complexity.
### Follow-up Questions
- How would you guard against `log(0)` producing an infinite loss?
- How would you generalize this to multiclass `predict_proba` output?
Overview: Compute binary log-loss in NumPy from the two-column probability matrix that a logistic regression's predict_proba returns. It tests vectorized indexing by label, the link to the binary cross-entropy formula, and handling of zero probabilities.
Compute Binary Log-Loss From Two-Column predict_proba Output
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0
Implement log_loss_two_column(y_true, proba) to compute binary log-loss from the two-column matrix returned by LogisticRegression.predict_proba. For each sample, take -log (natural log) of the probability its row assigns to the true class, and return the mean as a Python float. Use column 1 when the label is 1 and column 0 when the label is 0.