LinearSVC
Linear Support Vector Classification using liblinear-style dual coordinate descent and primal L-BFGS / coordinate descent.
Overview
LinearSVC implements linear Support Vector Classification for binary and multiclass (one-vs-rest) datasets. Unlike kernelized SVC(kernel="linear"), LinearSVC solves the linear maximum-margin problem directly via liblinear-style dual coordinate descent or primal L-BFGS / coordinate descent, scaling linearly in the number of samples.
When dual="auto", OchreML selects:
dual=Trueforloss="hinge"dual=Falseforpenalty="l1"dual=(n_samples < n_features)forpenalty="l2"withloss="squared_hinge"
When to Use
- High-dimensional or large-sample linear classification (text TF-IDF features, tabular datasets) where maximum-margin hyperplanes perform strongly.
- Sparse linear SVM feature selection via
penalty="l1", loss="squared_hinge", dual=False. - When calibrated probability estimates are needed instead of margin scores, prefer
LogisticRegression.
Hardware Backend Support
| Backend | Primitives Used | Support |
|---|---|---|
| CPU (Rayon) | Dense E1-E5, N2 (f64) | Full (default fallback) |
CUDA (cuda:0, cuda:all) | cuBLAS FP64 matvec & primal gradients (E1-E5, N2) | Full |
wgpu (wgpu) | WGSL compute shaders (E1-E5, N2) | Full |
TPU (tpu:0) | OpenXLA PJRT (E1-E5, N2) | Full |
Import & Constructor
from ochreml.svm import LinearSVC
# or top-level lazy import:
from ochreml import LinearSVC
clf = LinearSVC(
penalty: str = "l2",
loss: str = "squared_hinge",
*,
dual: str | bool = "auto",
tol: float = 1e-4,
C: float = 1.0,
fit_intercept: bool = True,
intercept_scaling: float = 1.0,
class_weight: dict | str | None = None,
random_state: int | None = None,
max_iter: int = 1000,
device: str | None = None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
penalty | "l1" | "l2" | "l2" | Norm used in the regularization penalty. |
loss | "hinge" | "squared_hinge" | "squared_hinge" | Margin loss function ("hinge" is standard SVM loss; "squared_hinge" is its smooth square). |
dual | "auto" | bool | "auto" | Keyword-only. Whether to solve the dual or primal optimization problem. |
tol | float | 1e-4 | Keyword-only. Tolerance for stopping criteria. |
C | float | 1.0 | Keyword-only. Inverse regularization strength; must be strictly positive. |
fit_intercept | bool | True | Keyword-only. Whether to fit an intercept term. |
intercept_scaling | float | 1.0 | Keyword-only. Synthetic feature value appended when fit_intercept=True to reduce regularization on the intercept. |
class_weight | dict | "balanced" | None | None | Keyword-only. Class weights ("balanced" or {label: weight}). |
random_state | int | None | None | Keyword-only. Seed controlling coordinate shuffling in dual coordinate descent. |
max_iter | int | 1000 | Keyword-only. Maximum number of solver iterations. |
device | str | None | None | Keyword-only. Target compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type | Description |
|---|---|---|
coef_ | ndarray of shape (1, n_features) or (n_classes, n_features) | Weights assigned to the features. |
intercept_ | ndarray of shape (1,) or (n_classes,) | Constants in the decision function. |
classes_ | ndarray of shape (n_classes,) | Sorted unique class labels seen during fit. |
n_features_in_ | int | Number of features seen during fit. |
n_iter_ | int | Number of iterations executed by the solver. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y, sample_weight=None) -> self | Fit linear SVM classifier on (X, y). |
decision_function | decision_function(X) -> ndarray | Predict signed distance / confidence scores of shape (n_samples,) or (n_samples, n_classes). |
predict | predict(X) -> ndarray | Predict class labels for samples in X. |
score | score(X, y) -> float | Return mean classification accuracy on (X, y). |
to | to(device: str) -> self | Move the estimator execution target to another hardware device. |
save / load | clf.save(path) / LinearSVC.load(path, device=None) | Persist or restore the model in OchreML's binary .dit format. |
Usage Example
import numpy as np
import ochreml
from ochreml.svm import LinearSVC
rng = np.random.default_rng(42)
X0 = rng.normal(loc=-1.5, scale=0.8, size=(200, 4))
X1 = rng.normal(loc=1.5, scale=0.8, size=(200, 4))
X = np.vstack([X0, X1])
y = np.array([0] * 200 + [1] * 200, dtype=np.float64)
clf = LinearSVC(
penalty="l2",
loss="squared_hinge",
C=1.0,
dual="auto",
random_state=42,
device="auto",
)
clf.fit(X, y)
scores = clf.decision_function(X[:5])
preds = clf.predict(X[:5])
print(f"Accuracy: {clf.score(X, y):.4f}, n_iter_: {clf.n_iter_}")
# Persist to .dit and reload
clf.save("linear_svc.dit")
loaded = ochreml.load("linear_svc.dit", device="cpu")
assert np.array_equal(preds, loaded.predict(X[:5]))TweedieRegressor
Generalized Linear Model with a flexible Tweedie power variance family (Normal, Poisson, Compound Poisson-Gamma, Gamma, Inverse Gaussian).
LinearSVR
Linear Support Vector Regression with epsilon-insensitive or squared epsilon-insensitive loss via dual coordinate descent and primal L-BFGS.