Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

LinearSVC / LinearSVR

API Reference

Signature

clf = sp.LinearSVC(
    c=1.0, max_iter=1000, tol=1e-4, fit_intercept=True
)
reg = sp.LinearSVR(
    c=1.0, epsilon=0.1, max_iter=1000, tol=1e-4, fit_intercept=True
)

model.fit(X, y)
model.predict(X)               -> list[int] | list[float]
model.score(X, y)              -> float
clf.decision_function(X)       -> list[float]   # LinearSVC only
model.get_params()             -> dict
model.set_params(c=..., max_iter=..., tol=...)

Constructor parameters — LinearSVC

ParameterTypeDefaultDescription
cfloat1.0Inverse regularisation strength (smaller = stronger)
max_iterint1000Maximum training epochs
tolfloat1e-4Convergence tolerance
fit_interceptboolTrueFit a bias term

Constructor parameters — LinearSVR

ParameterTypeDefaultDescription
cfloat1.0Inverse regularisation strength
epsilonfloat0.1Half-width of the $\varepsilon$-insensitive tube
max_iterint1000Maximum training epochs
tolfloat1e-4Convergence tolerance
fit_interceptboolTrueFit a bias term

Attributes

AttributeTypeDescription
classes_list[int]Unique class labels (LinearSVC only)
coef_listWeight coefficients — shape (K, p) for multiclass SVC, (p,) for SVR
intercept_float | list[float]Bias term(s)
C_floatRegularisation parameter
Example
import seraplot as sp
import numpy as np

X = np.random.randn(500, 4)
y = (X[:, 0] + X[:, 1] > 0).astype(int)

clf = sp.LinearSVC(c=1.0, fit_intercept=True)
clf.fit(X, y)
print(f"Accuracy: {clf.score(X, y):.4f}")
print(f"Margins: {clf.decision_function(X[:3])}")

reg = sp.LinearSVR(c=1.0, epsilon=0.05)
reg.fit(X, X[:, 0] * 2 - X[:, 2])
print(f"R²: {reg.score(X, X[:, 0] * 2 - X[:, 2]):.4f}")

Algorithmic Functioning

Both models are solved via the dual coordinate descent method on the kernelised SVM dual problem restricted to a linear kernel.

LinearSVC — Primal objective (hinge loss + L2 regularisation):

$$\min_{w, b} \frac{1}{2}\|w\|^2 + C \sum_{i=1}^n \max\!\bigl(0,\, 1 - y_i(w^\top x_i + b)\bigr)$$

The constraint $y_i(w^\top x_i + b) \geq 1$ defines the margin; misclassified points contribute a hinge penalty.

Dual form — introduce per-sample dual variables $\alpha_i \in [0, C]$:

$$\max_{\alpha} \sum_{i=1}^n \alpha_i - \frac{1}{2}\sum_{i,j} \alpha_i \alpha_j y_i y_j x_i^\top x_j \quad \text{s.t.} \quad \sum_i \alpha_i y_i = 0$$

Dual coordinate descent updates one $\alpha_i$ at a time, solving the 1-d quadratic subproblem analytically with clipping to $[0, C]$.

Prediction — signed margin:

$$f(x) = w^\top x + b = \sum_{i} \alpha_i y_i x_i^\top x + b$$

For multiclass (OvR): $K$ binary SVMs are trained, and the class with the highest margin wins.

LinearSVR — Primal objective ($\varepsilon$-insensitive loss):

$$\min_{w, b} \frac{1}{2}\|w\|^2 + C \sum_{i=1}^n \max\!\bigl(0,\, |y_i - (w^\top x_i + b)| - \varepsilon\bigr)$$

Residuals smaller than $\varepsilon$ incur zero penalty — the model ignores small errors and focuses on larger deviations.

Référence API

Signature

clf = sp.LinearSVC(
    c=1.0, max_iter=1000, tol=1e-4, fit_intercept=True
)
reg = sp.LinearSVR(
    c=1.0, epsilon=0.1, max_iter=1000, tol=1e-4, fit_intercept=True
)

model.fit(X, y)
model.predict(X)               -> list[int] | list[float]
model.score(X, y)              -> float
clf.decision_function(X)       -> list[float]   # LinearSVC seulement
model.get_params()             -> dict
model.set_params(c=..., max_iter=..., tol=...)

Paramètres du constructeur — LinearSVC

ParamètreTypeDéfautDescription
cfloat1.0Inverse de la force de régularisation (plus petit = plus fort)
max_iterint1000Nombre maximum d'époques d'entraînement
tolfloat1e-4Tolérance de convergence
fit_interceptboolTrueAjuster un terme de biais

Paramètres du constructeur — LinearSVR

ParamètreTypeDéfautDescription
cfloat1.0Inverse de la force de régularisation
epsilonfloat0.1Demi-largeur du tube $\varepsilon$-insensible
max_iterint1000Nombre maximum d'époques d'entraînement
tolfloat1e-4Tolérance de convergence
fit_interceptboolTrueAjuster un terme de biais

Attributs

AttributTypeDescription
classes_list[int]Labels de classes uniques (LinearSVC seulement)
coef_listCoefficients de poids — forme (K, p) pour SVC multiclasse, (p,) pour SVR
intercept_float | list[float]Terme(s) de biais
C_floatParamètre de régularisation
Exemple
import seraplot as sp
import numpy as np

X = np.random.randn(500, 4)
y = (X[:, 0] + X[:, 1] > 0).astype(int)

clf = sp.LinearSVC(c=1.0, fit_intercept=True)
clf.fit(X, y)
print(f"Précision : {clf.score(X, y):.4f}")
print(f"Marges : {clf.decision_function(X[:3])}")

reg = sp.LinearSVR(c=1.0, epsilon=0.05)
reg.fit(X, X[:, 0] * 2 - X[:, 2])
print(f"R² : {reg.score(X, X[:, 0] * 2 - X[:, 2]):.4f}")

Fonctionnement algorithmique

Les deux modèles sont résolus via la méthode de descente de coordonnées duale sur le problème dual SVM noyauté restreint au noyau linéaire.

LinearSVC — Objectif primal (perte hinge + régularisation L2) :

$$\min_{w, b} \frac{1}{2}\|w\|^2 + C \sum_{i=1}^n \max\!\bigl(0,\, 1 - y_i(w^\top x_i + b)\bigr)$$

La contrainte $y_i(w^\top x_i + b) \geq 1$ définit la marge ; les points mal classifiés contribuent une pénalité hinge.

Forme duale — introduire des variables duales $\alpha_i \in [0, C]$ par échantillon :

$$\max_{\alpha} \sum_{i=1}^n \alpha_i - \frac{1}{2}\sum_{i,j} \alpha_i \alpha_j y_i y_j x_i^\top x_j \quad \text{s.t.} \quad \sum_i \alpha_i y_i = 0$$

La descente de coordonnées duale met à jour un $\alpha_i$ à la fois, résolvant analytiquement le sous-problème quadratique 1-d avec écrêtage à $[0, C]$.

Prédiction — marge signée :

$$f(x) = w^\top x + b = \sum_{i} \alpha_i y_i x_i^\top x + b$$

Pour le multiclasse (OvR) : $K$ SVMs binaires sont entraînés, et la classe avec la marge la plus haute l'emporte.

LinearSVR — Objectif primal (perte $\varepsilon$-insensible) :

$$\min_{w, b} \frac{1}{2}\|w\|^2 + C \sum_{i=1}^n \max\!\bigl(0,\, |y_i - (w^\top x_i + b)| - \varepsilon\bigr)$$

Les résidus plus petits que $\varepsilon$ n'entraînent aucune pénalité — le modèle ignore les petites erreurs et se concentre sur les déviations plus grandes.