Skip to Content
OchreML
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 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 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

ParameterTypeDefaultDescription
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.
tolfloat1e-3Tolerance for the SMO stopping criterion.
nufloat0.5Upper bound on the fraction of training errors and lower bound on the fraction of support vectors. Must be in (0, 1].
max_iterint-1Hard limit on solver iterations, or -1 for no limit.
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.
dual_coef_ndarray of shape (1, n_SV)Dual coefficients of the support vectors.
intercept_ndarray of shape (1,)Constant intercept term (-offset_).
offset_floatOffset used to define the decision function from raw scores (decision_function(X) = score_samples(X) - offset_).
n_features_in_intNumber of features seen during fit().
device_str | NoneResolved execution device string.

Methods

MethodSignatureDescription
fitfit(X) -> OneClassSVMDetect the soft boundary of the training samples X.
score_samplesscore_samples(X) -> ndarrayReturn the raw unshifted scoring function for samples in X.
decision_functiondecision_function(X) -> ndarrayReturn the signed distance to the separating hyperplane (positive for inliers, negative for outliers).
predictpredict(X) -> ndarrayClassify samples in X as +1.0 (inlier) or -1.0 (outlier).
toto(device) -> OneClassSVMMove the estimator's compute target to another device.
savesave(path) -> NoneSave the fitted model to a .dit binary file.
loadOneClassSVM.load(path, device=None) -> OneClassSVMClass 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_)

On this page