Soft-Margin Dual SVM Solver with SciPy SLSQP
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement a linear soft-margin SVM by solving its dual with `scipy.optimize.minimize(method="SLSQP")`. Maximize `sum(alpha) - 0.5 * alpha @ Q @ alpha` with `Q = outer(y, y) * (X @ X.T)`, subject to `0 <= alpha <= C` and `alpha @ y == 0`.
Write three functions:
- `dual_objective(alpha, X, y)` returns the dual objective as a `float`.
- `fit_dual_svm(X, y, C, sv_tol=1e-6)` returns `(alpha, w, b, support)`: `alpha` clipped to `[0, C]`, `support = np.flatnonzero(alpha > sv_tol)`, and `w = sum(alpha_i * y_i * x_i)` over the support vectors.
- `b` is the mean of `y_i - x_i @ w` over the free support vectors (`sv_tol < alpha < C - sv_tol`). If none are free, average over all support vectors. If there are no support vectors, `b = 0.0`.
- `decision_function(X, w, b)` returns `X @ w + b` with shape `(n,)`.
The solution must satisfy the KKT conditions, and `w` must match `sklearn.svm.SVC(kernel="linear", C=C)` to within `1e-3`. `b` must match to the same tolerance whenever at least one support vector is free. With every support vector at the bound, `b` is not unique and the fallback rule above defines it.
Example:
```python
X = [[0, 0], [2, 2]]
y = [-1, 1]
fit_dual_svm(X, y, C=10.0)
# alpha=[0.25, 0.25], w=[0.5, 0.5], b=-1.0, support=[0, 1]
fit_dual_svm(X, y, C=0.1)
# alpha=[0.1, 0.1], w=[0.2, 0.2], b=-0.4 (all support vectors at the bound)
```
```hint Hand the solver the right problem
`minimize` only minimizes. Decide what objective to pass, and give it the analytic gradient of that objective and the Jacobian of the equality constraint so it can reach a tight tolerance.
```
```hint Which points pin the intercept
Complementary slackness says which support vectors satisfy `y_i * f(x_i) = 1` exactly. Those are the ones that determine `b`.
```
### Constraints & Assumptions
- Labels are `-1.0` / `+1.0`, and both classes are present.
- n ≤ 100, `C > 0`, linear kernel only.
- `alpha @ y` must be within `1e-7` of 0.
### What a Strong Answer Covers
- Building `Q`, negating the dual for a minimizer, and passing bounds, the equality constraint and analytic derivatives.
- Recovering `alpha`, the support set and `w` from the solver output, including clipping solver noise.
- Computing `b` from free support vectors with both fallbacks, and why bounded ones are only a fallback.
- Checking the result against the KKT conditions and a reference implementation, and knowing when `b` or `alpha` is not unique.
- The time and memory cost of a dense QP solver and where it stops being practical.
### Follow-up Questions
- How would you extend this to an RBF kernel?
- Why does SLSQP scale poorly, and what would you use for large n?
- Why average `b` over free support vectors only?
- How would you check the KKT conditions numerically on the returned solution?
Overview: Train a linear soft-margin SVM by solving its dual quadratic program with SciPy's SLSQP, then recover the weights, intercept and support vectors. It tests the SVM dual derivation, complementary slackness, constrained optimization in SciPy and checking a result against KKT conditions.