Compute the per-sample binary log-loss vector in NumPy
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `log_loss_per_sample(y_true, y_pred)` returning the unreduced per-sample log-loss vector. Entry i is `-(y_i*log(p_i) + (1-y_i)*log(1-p_i))` using the natural log.
Requirements:
- `y_true` is a 1-D array of 0/1 labels with shape `(n,)`. `y_pred` is a 1-D array of P(y=1) with shape `(n,)`, strictly inside `(0, 1)`.
- Return a NumPy array of shape `(n,)` with dtype `float64`.
- Keep it vectorized, with no `mean` or `sum` and no `sklearn.metrics.log_loss`.
Example:
```text
y_true = [1, 0, 1, 1, 0]
y_pred = [0.9, 0.1, 0.8, 0.6, 0.3]
Output: [0.10536052, 0.10536052, 0.22314355, 0.51082562, 0.35667494]
```
The first two entries are equal because each is a correct prediction made with probability 0.9, which costs $-\ln 0.9$ whichever class it is for.
```hint Which term survives
Look at which of the two terms is left for each label value, and make sure both inputs really have shape `(n,)` before you combine them.
```
### Constraints & Assumptions
- Both inputs are 1-D with the same length n, and every `y_pred` value lies strictly inside (0, 1), so no clipping is required.
- Inputs may arrive as Python lists or integer arrays, as in the example.
### What a Strong Answer Covers
- A vectorized element-wise implementation of the formula that returns a `float64` vector of shape `(n,)` with no reduction.
- Why each entry reduces to $-\ln p_i$ or $-\ln(1 - p_i)$, and how the mean of the vector relates to the logistic regression cost.
- Input handling: converting lists and integer labels, and guarding against shape mismatches that broadcast silently.
- What changes when predictions can reach exactly 0 or 1, or when the model outputs logits.
### Follow-up Questions
- How would you make the function safe when `y_pred` can be exactly 0 or 1?
- How would you compute the same loss from logits without losing precision?
- Why is the per-sample vector more useful than the mean when debugging a model?
- Does the formula still make sense for soft labels between 0 and 1?
Overview: Implement the unreduced binary cross-entropy in NumPy, returning one log-loss value per sample. It tests the log-loss formula, vectorized array handling, and what breaks when predictions reach 0 or 1 or arrive as logits.