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 parallelf64),cuda/cuda:0(dynamic cuBLAS),wgpu(Vulkan/Metal/DX12),tpu(OpenXLA PJRT), andauto - Persistence:
.ditbinary format viasave()/load()(andochreml.save()/ochreml.load()), plus Pythonpicklesupport
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
| Parameter | Type | Default | Description |
|---|---|---|---|
C | float | 1.0 | Regularization parameter. Must be strictly positive (C > 0). |
kernel | {"linear", "poly", "rbf", "sigmoid", "precomputed"} | "rbf" | Kernel function used to compute the Gram matrix. |
degree | int | 3 | Degree 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. |
coef0 | float | 0.0 | Independent term in "poly" and "sigmoid" kernel functions. |
probability | bool | False | Whether to enable probability estimates via Platt scaling. Must be True before calling predict_proba(). |
tol | float | 1e-3 | Tolerance for the SMO stopping criterion. |
max_iter | int | -1 | Hard limit on solver iterations, or -1 for no limit. |
random_state | int | None | None | Random seed used for probability calibration. |
device | str | None | None | Target compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type | Description |
|---|---|---|
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_ | int | Number of features seen during fit(). |
device_ | str | None | Resolved execution device string. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y) -> SVC | Fit the SVM classifier on feature matrix X and labels y. |
decision_function | decision_function(X) -> ndarray | Evaluate the decision function for samples in X (1D for binary, 2D for multiclass). |
predict | predict(X) -> ndarray | Predict class labels for samples in X. |
predict_proba | predict_proba(X) -> ndarray | Return calibrated class probability estimates (requires probability=True). |
score | score(X, y) -> float | Return mean classification accuracy on (X, y). |
to | to(device) -> SVC | Move the estimator's compute target to another device. |
save | save(path) -> None | Save the fitted model to a .dit binary file. |
load | SVC.load(path, device=None) -> SVC | Class 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_)