Skip to Content
OchreML
Linear, GLM & Linear SVM

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=True for loss="hinge"
  • dual=False for penalty="l1"
  • dual=(n_samples < n_features) for penalty="l2" with loss="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

BackendPrimitives UsedSupport
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

ParameterTypeDefaultDescription
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.
tolfloat1e-4Keyword-only. Tolerance for stopping criteria.
Cfloat1.0Keyword-only. Inverse regularization strength; must be strictly positive.
fit_interceptboolTrueKeyword-only. Whether to fit an intercept term.
intercept_scalingfloat1.0Keyword-only. Synthetic feature value appended when fit_intercept=True to reduce regularization on the intercept.
class_weightdict | "balanced" | NoneNoneKeyword-only. Class weights ("balanced" or {label: weight}).
random_stateint | NoneNoneKeyword-only. Seed controlling coordinate shuffling in dual coordinate descent.
max_iterint1000Keyword-only. Maximum number of solver iterations.
devicestr | NoneNoneKeyword-only. Target compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device().

Attributes

AttributeTypeDescription
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_intNumber of features seen during fit.
n_iter_intNumber of iterations executed by the solver.

Methods

MethodSignatureDescription
fitfit(X, y, sample_weight=None) -> selfFit linear SVM classifier on (X, y).
decision_functiondecision_function(X) -> ndarrayPredict signed distance / confidence scores of shape (n_samples,) or (n_samples, n_classes).
predictpredict(X) -> ndarrayPredict class labels for samples in X.
scorescore(X, y) -> floatReturn mean classification accuracy on (X, y).
toto(device: str) -> selfMove the estimator execution target to another hardware device.
save / loadclf.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]))

On this page