Empirical Nu-Property Sweep for NuSVC and OneClassSVM
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `nusvc_nu_sweep(X, y, nus, gamma=1.0)` and `one_class_nu_sweep(X, nus, gamma=1.0)`, which fit one RBF model per `nu` (in input order) and return a `pd.DataFrame` with one row per `nu`. Also implement `nu_bounds_satisfied(table, error_column, atol=1e-9)`, returning a per-row boolean array.
The sweep checks the ν-property on real fits: for each `nu`, it records how the support-vector fraction and the margin-error (or outlier) fraction compare with `nu`.
Requirements:
- Fit with `kernel="rbf"`, the given `gamma`, `tol=1e-6`. Columns: `nu` (float64), `n_support` (int64, `len(support_)`), `sv_fraction` (`n_support / n`), then the error column (float64).
- NuSVC error column `margin_error_fraction`: fraction with `y * decision_function(X) < 1 - 1e-3`.
- OneClassSVM error column `outlier_fraction`: fraction with `decision_function(X) < -1e-3`.
- `nu_bounds_satisfied`: row is `True` iff `error <= nu + atol` and `sv_fraction >= nu - atol`.
- Raise `ValueError` if `nus` is empty or any `nu` is non-finite or outside `(0, 1]`, or if `y` is not exactly the labels `{-1, +1}`.
Example:
```python
table = pd.DataFrame({
"nu": [0.2, 0.5, 0.5, 0.3],
"sv_fraction": [0.25, 0.4, 0.6, 0.3],
"margin_error_fraction": [0.1, 0.2, 0.55, 0.3],
})
nu_bounds_satisfied(table, "margin_error_fraction")
# -> array([ True, False, False, True])
```
```hint Validate before fitting
Check `nus` and `y` before the first fit. The interval is open at 0 and closed at 1, and NaN or infinity must be rejected too. For the labels, both -1 and +1 must be present and nothing else.
```
```hint Column dtypes
Collect one plain tuple per `nu` and cast the columns once at the end, so `n_support` comes out as int64 and every fraction as float64 no matter what pandas infers.
```
### Constraints & Assumptions
- `X` is a finite 2-D array of shape `(n, d)`, with `n` at most a few hundred.
- For NuSVC, `len(y) == n` and every `nu` is feasible for the class balance.
### What a Strong Answer Covers
- Input validation that rejects an empty `nus`, non-finite values, values outside `(0, 1]`, and any label set other than exactly `{-1, +1}`.
- One fit per `nu` in input order with `kernel="rbf"`, the given `gamma` and `tol=1e-6`, with support vectors counted from `support_`.
- Error and outlier counts taken from the training-set `decision_function` with the stated tolerances and divided by `n`.
- A DataFrame with the exact column names, order and dtypes, and a vectorized `nu_bounds_satisfied`.
- Why the ν-property holds for both models, and why a tolerance is needed when counting points near the margin.
### Follow-up Questions
- Why does `nu` bound both quantities, and when do the bounds become tight?
- Why use a tolerance like `1e-3` when counting margin errors?
- How does `nu` in NuSVC relate to `C` in SVC?
Overview: Implement sweeps over nu for scikit-learn's NuSVC and OneClassSVM that record the support-vector fraction and the margin-error or outlier fraction of each RBF fit, then check them against the nu-property bounds. It tests input validation, reading fitted SVM attributes, and the dual argument behind both bounds.