Support Vector Regression (SVR) extends the principles of Support Vector Machines (SVMs) from classification to regression problems. Developed from the foundation laid by Vapnik and others in the Support Vector Machine framework, SVR provides a unique approach to regression by focusing on fitting the error within a certain threshold and is particularly known for its robustness and effectiveness in high-dimensional spaces.
SVR is grounded in the concept of SVM, which constructs a hyperplane or set of hyperplanes in a high-dimensional space to separate different classes. For regression, SVR attempts to find a function that has at most deviation from the actually obtained targets for all the training data, and at the same time, is as flat as possible.
In SVR, the input data is mapped onto a high-dimensional feature space using a kernel function, and then a linear model is built in this feature space:
where is the weight vector, is the high-dimensional feature space mapped from the input vector , and is the bias. The goal is to minimize:
subject to the constraints:
Here, and are slack variables that measure the degree of violation of the -insensitive loss, is the regularization parameter, and defines the width of the tube within which predictions are considered acceptable without penalty.
The primal problem is often solved in its dual form to exploit kernel methods, leading to:
subject to:
Here, and are Lagrange multipliers, and is the kernel function.
C
: float
, default = 1.0epsilon
: float
, default = 0.1batch_size
: int
, default = 100learning_rate
: float
, default = 0.001max_iter
: int
, default = 1000from luma.regressor.svm import SVR
from luma.model_selection.search import GridSearchCV
from luma.metric.regression import MeanSquaredError
from luma.visual.evaluation import ResidualPlot
import matplotlib.pyplot as plt
import numpy as np
n_samples = 100
X = np.linspace(-3, 3, n_samples).reshape(-1, 1)
y = (2 * X + 3 * np.random.rand(n_samples, 1)).flatten()
param_grid = {"C": np.logspace(-3, 3, 5),
"learning_rate": np.logspace(-3, -1, 5)}
grid = GridSearchCV(estimator=SVR(),
param_grid=param_grid,
cv=5,
metric=MeanSquaredError,
maximize=False,
refit=True,
shuffle=True,
random_state=42,
verbose=True)
grid.fit(X, y)
print(grid.best_params, grid.best_score)
reg = grid.best_model
y_pred = reg.predict(X)
score = reg.score(X, y, metric=MeanSquaredError)
fig = plt.figure(figsize=(10, 5))
ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 2)
ax1.scatter(X, y, s=10, c="black", alpha=0.4)
ax1.plot(X, y_pred, lw=2, c="dodgerblue", label="Predicted Plot")
ax1.fill_between(X.flatten(), y, y_pred, color="dodgerblue", alpha=0.2)
ax1.set_xlabel("x")
ax1.set_ylabel("y")
ax1.set_title(f"{type(reg).__name__} Estimation [MSE: {score:.4f}]")
ax1.legend()
ax1.grid(alpha=0.2)
res = ResidualPlot(reg, X, y)
res.plot(ax=ax2, show=True)
- Vapnik, Vladimir. "The nature of statistical learning theory." Springer Science & Business Media, 2013.
- Smola, Alex J., and Bernhard Schölkopf. "A tutorial on support vector regression." Statistics and computing 14.3 (2004): 199-222.