SVR Epsilon and C Sweep
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement four functions that fit RBF `SVR` models, measure the epsilon-insensitive loss, classify each sample relative to the fitted tube, and summarise a grid sweep over `epsilon` and `C` as a DataFrame.
Requirements:
- `fit_svr(X, y, C, epsilon, gamma)`: return a fitted `SVR(kernel="rbf", ..., tol=1e-6)`.
- `epsilon_insensitive_loss(y, y_pred, epsilon)`: per-sample `max(0, |y - y_pred| - epsilon)`.
- `tube_status(y, y_pred, epsilon, margin=1e-3)`: with residual `r = y - y_pred`, return an int64 array with `0` if `|r| < epsilon - margin`, `2` if `|r| > epsilon + margin`, else `1` (boundary).
- `svr_sweep(X, y, epsilons, Cs, gamma)`: one row per fit, `epsilons` outer loop, `Cs` inner, columns exactly `SWEEP_COLUMNS`, default RangeIndex.
- Status counts and `mean_eps_loss` use training predictions. `inside_support` counts support vectors with status `0`. `epsilon`, `C`, `mean_eps_loss` are float64, the rest int64.
The sweep columns, in order:
```python
SWEEP_COLUMNS = ["epsilon", "C", "n_support", "n_inside", "n_boundary", "n_outside", "inside_support", "mean_eps_loss"]
```
`n_support` is the number of support vectors in the fit, and `n_inside`, `n_boundary` and `n_outside` count the training samples with status 0, 1 and 2.
Example:
```python
epsilon_insensitive_loss([1, 2, 3, 0], [1.05, 2.5, 2.0, 0.2], 0.2)
# -> [0.0, 0.3, 0.8, 0.0]
tube_status(np.zeros(5), [0.0, 0.5, -0.5004, 0.6, -0.49], 0.5)
# -> [0, 1, 1, 2, 0]
```
```hint A built-in check
Before writing svr_sweep, work out from the KKT conditions which tube statuses a support vector can have. That gives you a way to sanity-check your counts.
```
```hint Output shape
Build plain rows first, then set the column order and dtypes on the finished DataFrame. Do not rely on pandas to infer int64 and float64 from Python values.
```
### Constraints & Assumptions
- `X` has shape `(n, d)` and `y` has length `n`, both numeric.
- `C > 0`, `epsilon >= 0`, and `gamma` is a positive float passed to the RBF kernel.
- `margin` is an absolute tolerance on `|r|` and defaults to `1e-3`.
- The sweep fits `len(epsilons) * len(Cs)` models, each on the full training set.
### What a Strong Answer Covers
- Vectorized loss and tube classification that follow the boundary rule exactly and return int64 labels.
- A sweep in the required loop order that predicts on the training data and counts statuses and support vectors.
- Exact column order, float64 and int64 dtypes, and a default RangeIndex.
- The KKT link between a sample's tube status and its dual coefficients, and what `inside_support` should be for a converged fit.
- Why both a tight `tol` and a nonzero `margin` are needed.
### Follow-up Questions
- Why do points strictly inside the tube never become support vectors?
- Why is a tolerance `margin` needed when classifying boundary points?
- How does increasing `C` affect the fit for a fixed `epsilon`?
Overview: Write Python functions that fit RBF support vector regression models over a grid of epsilon and C values, compute the epsilon-insensitive loss, and label each training sample as inside, on or outside the fitted tube. Tests scikit-learn SVR usage, KKT reasoning about support vectors and careful DataFrame output.