Sub topic 16
Support vector regression
This chapter assumes you know the soft-margin SVM objective, slack variables, the role of C, and the kernel trick from the earlier chapters. It also assumes you can use scikit-learn pipelines.
From a margin to a tube
Support vector regression (SVR) predicts a number instead of a class. It reuses the SVM machinery with one change: the loss function.
A classifier penalizes points that fall inside the margin or on the wrong side. A regressor penalizes points that fall too far from the prediction. SVR fits a function f(x) = w·x + b and draws a tube of half-width ε around it. Points inside the tube cost nothing. Points outside pay a linear penalty:
loss = max(0, |y − f(x)| − ε)
This is the ε-insensitive loss. Each point outside the tube gets a slack value, ξ if it lies above the tube and ξ* if it lies below. The primal problem is:
minimize 0.5·||w||² + C · Σ(ξᵢ + ξᵢ*)
The first term keeps the function flat, which is the regression version of a wide margin. The second term charges for points outside the tube. C plays the same role as in classification: a larger C punishes violations harder and allows a more flexible fit.
Support vectors in regression
Only points on the tube boundary or outside it become support vectors. Points strictly inside the tube have zero dual coefficients and do not affect the fit. A larger ε therefore gives a sparser, smoother model. A smaller ε gives a model that follows the data closely, and nearly every point becomes a support vector.
The dual problem depends only on dot products between inputs, so kernels work unchanged. The RBF kernel with its gamma parameter gives a nonlinear regression curve.
Three hyperparameters
| Parameter | Effect when increased |
|---|---|
| C | Fits training points more tightly, with higher variance |
| epsilon | Wider tube, fewer support vectors, smoother fit |
| gamma (RBF) | More local, wigglier fit |
Example in scikit-learn
Both the features and the target need scaling. The value of ε is in the units of y, so an unscaled target makes the default ε = 0.1 meaningless. TransformedTargetRegressor scales y for fitting and converts predictions back.
import numpy as np
from sklearn.compose import TransformedTargetRegressor
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR
rng = np.random.default_rng(0)
X = np.sort(rng.uniform(0, 6, size=(200, 1)), axis=0)
y = 100 * np.sin(X).ravel() + rng.normal(scale=10, size=200)
model = TransformedTargetRegressor(
regressor=make_pipeline(
StandardScaler(),
SVR(kernel="rbf", C=10, epsilon=0.1, gamma="scale"),
),
transformer=StandardScaler(),
)
model.fit(X, y)
svr = model.regressor_[-1]
print("support vectors:", len(svr.support_), "of", len(X))
print("R^2 on training data:", model.score(X, y))Here ε = 0.1 applies to the standardized target, so it is about a tenth of a standard deviation of y.
Common errors
- Unscaled target. ε and C then act on an arbitrary scale. A target in the thousands with ε = 0.1 turns almost every point into a support vector.
- ε too large. The tube swallows the signal and the model underfits.
- Tuning only C and gamma. Add ε to the grid search, and keep scaling inside the pipeline so each fold fits its own scalers.
- Large datasets. Kernel SVR training grows faster than linearly with the number of samples. For a linear model on many samples, use
LinearSVR, orSGDRegressorwithloss="epsilon_insensitive".
Exercise
You train an SVR on house prices in dollars (values around 300000) using the default epsilon=0.1, and almost every training point becomes a support vector. Why does this happen, and what are two fixes?
Show answer
Epsilon is measured in the units of the target, so a tube of half-width 0.1 dollars is far narrower than the noise, and nearly every point falls outside it. Fixes: scale the target (for example with TransformedTargetRegressor and StandardScaler) so epsilon is meaningful, or choose epsilon explicitly in dollars, ideally tuning it by cross-validation along with C and gamma.