Margin-Based Classification Surrogate Losses
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Given an array of margins `m = y*f(x)`, implement the per-example 0-1, hinge, squared hinge and logistic losses, their (sub)gradients with respect to `m`, and a `pandas` table comparing all four losses. Every function returns a float array the same shape as its input.
- `zero_one_loss(margins)`: `1.0` where `m <= 0`, else `0.0`.
- `hinge_loss(margins)`: `max(0, 1-m)`.
- `squared_hinge_loss(margins)`: `max(0, 1-m)**2`.
- `logistic_loss(margins)`: `log(1 + exp(-m))` with the natural log.
- `loss_gradient(name, margins)` with `name` in `"zero_one"` (always 0), `"hinge"` (`-1` where `m < 1`, else 0), `"squared_hinge"` and `"logistic"`.
- `surrogate_table(margins)`: a `DataFrame` with float columns `margin, zero_one, hinge, squared_hinge, logistic` in that order, one row per (flattened) margin, and the default index.
For example:
```python
m = [-1.0, 0.0, 1.0, 3.0]
surrogate_table(m)
# margin zero_one hinge squared_hinge logistic
# 0 -1.0 1.0 2.0 4.0 1.313262
# 1 0.0 1.0 1.0 1.0 0.693147
# 2 1.0 0.0 0.0 0.0 0.313262
# 3 3.0 0.0 0.0 0.0 0.048587
loss_gradient("squared_hinge", [-2.0, 0.0, 1.0]) # [-6., -2., 0.]
```
```hint Extreme margins
Evaluate the logistic formula at m = -1000 and at m = 1000 in float64 before you trust it, for both the loss and its gradient. Look for a way to take the log of a sum of exponentials without forming the exponentials.
```
### Constraints & Assumptions
- `margins` may be a scalar, a list or a NumPy array of any shape. The loss and gradient functions keep that shape; `surrogate_table` flattens it.
- Margins can be as extreme as `±1000`. The logistic loss and its gradient must stay finite there.
- An unknown `name` in `loss_gradient` raises `ValueError`.
### What a Strong Answer Covers
- Correct elementwise formulas for all four losses, including the convention that a zero margin counts as a 0-1 error.
- The subgradient convention at the hinge kink and a squared hinge gradient that is continuous at m = 1.
- A logistic loss and gradient that stay finite at `±1000`, with the reason the naive formula fails.
- Dispatch on `name` with `ValueError`, preserved shapes, and a table with the exact column order.
### Follow-up Questions
- Why is the 0-1 loss not minimized directly in training, and what do the three surrogates have in common that makes them usable?
- How do the hinge and logistic losses treat points that are already classified correctly with a large margin, and how does that show up in SVMs versus logistic regression?
- Which of the three surrogates is most sensitive to a mislabeled point with a very negative margin, and why?
Overview: Implement the 0-1, hinge, squared hinge and logistic losses as functions of the classification margin, with their subgradients and a pandas table comparing them. It tests kink conventions, a numerically stable logistic loss, and vectorized NumPy code.