Kernel, MLP & Gaussian Process
OneClassSVM
Unsupervised outlier and novelty detection using Scholkopf One-Class SVM with dual SMO.
Overview
OneClassSVM implements unsupervised outlier and novelty detection via the Scholkopf One-Class Support Vector Machine formulation. It estimates the soft boundary enclosing the support of a high-dimensional distribution using dual Sequential Minimal Optimization (SMO) over hardware-accelerated kernel Gram matrices. Samples predicted as +1 are inliers and -1 are outliers. (^▽^)
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 OneClassSVM
# Also available from top-level: from ochreml import OneClassSVM
OneClassSVM(
kernel: str = "rbf",
degree: int = 3,
gamma: str | float = "scale",
coef0: float = 0.0,
tol: float = 1e-3,
nu: float = 0.5,
max_iter: int = -1,
device: str | None = None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
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. |
tol | float | 1e-3 | Tolerance for the SMO stopping criterion. |
nu | float | 0.5 | Upper bound on the fraction of training errors and lower bound on the fraction of support vectors. Must be in (0, 1]. |
max_iter | int | -1 | Hard limit on solver iterations, or -1 for no limit. |
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. |
dual_coef_ | ndarray of shape (1, n_SV) | Dual coefficients of the support vectors. |
intercept_ | ndarray of shape (1,) | Constant intercept term (-offset_). |
offset_ | float | Offset used to define the decision function from raw scores (decision_function(X) = score_samples(X) - offset_). |
n_features_in_ | int | Number of features seen during fit(). |
device_ | str | None | Resolved execution device string. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X) -> OneClassSVM | Detect the soft boundary of the training samples X. |
score_samples | score_samples(X) -> ndarray | Return the raw unshifted scoring function for samples in X. |
decision_function | decision_function(X) -> ndarray | Return the signed distance to the separating hyperplane (positive for inliers, negative for outliers). |
predict | predict(X) -> ndarray | Classify samples in X as +1.0 (inlier) or -1.0 (outlier). |
to | to(device) -> OneClassSVM | Move the estimator's compute target to another device. |
save | save(path) -> None | Save the fitted model to a .dit binary file. |
load | OneClassSVM.load(path, device=None) -> OneClassSVM | Class method to load a fitted model from a .dit file. |
Example
import numpy as np
from ochreml.svm import OneClassSVM
rng = np.random.default_rng(0)
X_inliers = rng.normal(loc=0.0, scale=0.4, size=(80, 2))
detector = OneClassSVM(kernel="rbf", gamma="scale", nu=0.05)
detector.fit(X_inliers)
X_test = np.array([
[0.1, -0.1], # inlier
[5.0, 5.0], # outlier
], dtype=np.float64)
print("Predictions (+1 inlier, -1 outlier):", detector.predict(X_test))
print("Decision scores:", detector.decision_function(X_test))
print("Offset:", detector.offset_)