Skip to Content
OchreML
Kernel, MLP & Gaussian Process

SVC

C-Support Vector Classification with LIBSVM-style WSS3 SMO, One-vs-One multiclass decomposition, and optional Platt scaling.

Overview

SVC implements C-Support Vector Classification using LIBSVM-style Working Set Selection 3 (WSS3) Sequential Minimal Optimization (SMO) in Rust. For multiclass problems, it decomposes the task into binary One-vs-One (OvO) classifiers, and optionally calibrates class probabilities via Platt scaling when probability=True. (^▽^)

Backend Support

  • Primitives: N3 (pairwise L2 distance for RBF kernels), E1 (dense matrix multiplication for linear, polynomial, and sigmoid Gram matrices)
  • Devices: cpu (Rayon parallel f64), cuda / cuda:0 (dynamic cuBLAS), wgpu (Vulkan/Metal/DX12), tpu (OpenXLA PJRT), and auto
  • Persistence: .dit binary format via save() / load() (and ochreml.save() / ochreml.load()), plus Python pickle support

Import & Signature

from ochreml.svm import SVC
# Also available from top-level: from ochreml import SVC

SVC(
    C: float = 1.0,
    kernel: str = "rbf",
    degree: int = 3,
    gamma: str | float = "scale",
    coef0: float = 0.0,
    probability: bool = False,
    tol: float = 1e-3,
    max_iter: int = -1,
    random_state: int | None = None,
    device: str | None = None,
)

Parameters

ParameterTypeDefaultDescription
Cfloat1.0Regularization parameter. Must be strictly positive (C > 0).
kernel{"linear", "poly", "rbf", "sigmoid", "precomputed"}"rbf"Kernel function used to compute the Gram matrix.
degreeint3Degree of the polynomial kernel (kernel="poly"). Ignored by other kernels.
gamma{"scale", "auto"} or float"scale"Kernel coefficient for "rbf", "poly", and "sigmoid". "scale" uses 1 / (n_features * X.var()); "auto" uses 1 / n_features.
coef0float0.0Independent term in "poly" and "sigmoid" kernel functions.
probabilityboolFalseWhether to enable probability estimates via Platt scaling. Must be True before calling predict_proba().
tolfloat1e-3Tolerance for the SMO stopping criterion.
max_iterint-1Hard limit on solver iterations, or -1 for no limit.
random_stateint | NoneNoneRandom seed used for probability calibration.
devicestr | NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device().

Attributes

AttributeTypeDescription
support_ndarray of shape (n_SV,)Indices of support vectors in the training set.
support_vectors_ndarray of shape (n_SV, n_features)Support vectors extracted during fitting.
n_support_ndarray of shape (n_classes,)Number of support vectors for each class.
dual_coef_ndarray of shape (n_classes - 1, n_SV)Dual coefficients of the support vectors in the decision function.
intercept_ndarray of shape (n_classes * (n_classes - 1) / 2,)Constants in the binary decision functions.
classes_ndarray of shape (n_classes,)Sorted unique class labels seen during fit().
n_features_in_intNumber of features seen during fit().
device_str | NoneResolved execution device string.

Methods

MethodSignatureDescription
fitfit(X, y) -> SVCFit the SVM classifier on feature matrix X and labels y.
decision_functiondecision_function(X) -> ndarrayEvaluate the decision function for samples in X (1D for binary, 2D for multiclass).
predictpredict(X) -> ndarrayPredict class labels for samples in X.
predict_probapredict_proba(X) -> ndarrayReturn calibrated class probability estimates (requires probability=True).
scorescore(X, y) -> floatReturn mean classification accuracy on (X, y).
toto(device) -> SVCMove the estimator's compute target to another device.
savesave(path) -> NoneSave the fitted model to a .dit binary file.
loadSVC.load(path, device=None) -> SVCClass method to load a fitted model from a .dit file.

Example

import numpy as np
from ochreml.svm import SVC

X_train = np.array([
    [-2.0, -1.5],
    [-1.5, -2.0],
    [-1.0, -1.0],
    [1.0, 1.0],
    [1.5, 2.0],
    [2.0, 1.5],
], dtype=np.float64)
y_train = np.array([0, 0, 0, 1, 1, 1], dtype=np.float64)

clf = SVC(C=1.0, kernel="rbf", gamma="scale", probability=True, random_state=42)
clf.fit(X_train, y_train)

X_test = np.array([[-1.2, -1.4], [1.4, 1.2]], dtype=np.float64)
print("Predictions:", clf.predict(X_test))
print("Probabilities:", clf.predict_proba(X_test))
print("Train accuracy:", clf.score(X_train, y_train))
print("Support vectors:", clf.n_support_)

On this page