One-class SVM and residuals
Quick answer
One-class SVM estimates a boundary around reference observations in feature space. Robust residual rules instead flag unusually large deviations from an expected measurement, often using the median and median absolute deviation (MAD). Examples are static Python snippets tested locally on CPU; Native detector examples require NumPy and scikit-learn; exercises use standard-library Python. ByteMind execution is unverified.
Median absolute deviation
from statistics import medianr=[0,1,1,2,100];m=median(r)print(m,median(abs(x-m) for x in r))Expected output (locally tested): 1 1. A single large residual does not dominate the median-based scale.
How it works
OneClassSVM with an RBF kernel uses similarity exp(-gamma*||x-z||^2) and learns a separating boundary relative to the origin in feature space. nu is an upper bound on the fraction of training errors and a lower bound on the fraction of support vectors under the standard formulation, subject to numerical tolerance; it is not a future false-alarm target. gamma controls the spatial reach of the kernel. Standardize features on training rows inside a pipeline because distances depend on scale. Its decision_function is positive for inliers, negative for outliers. It can be sensitive to contamination and costly on large samples. A residual e=y-f(x) compares an observed reading with a prediction from information available before that reading. Use signed residuals for direction or |e| for magnitude. The robust center m=median(e_train) and MAD=median(|e_train-m|) resist isolated extremes. Under a normal model, 1.4826MAD approximates standard deviation; this is not universal. Define a robust standardized score |e-m|/(1.4826MAD). If MAD=0, do not divide by zero: use a documented sensor tolerance or abstain and investigate. A fixed residual rule is interpretable but can miss joint multivariate changes. Compare both detectors at a calibration-only operating point and report actual false alarms on later normals.
One-class SVM on normal reference
from sklearn.pipeline import make_pipelinefrom sklearn.preprocessing import StandardScalerfrom sklearn.svm import OneClassSVMx=[[-1],[-.5],[0],[.5],[1]]m=make_pipeline(StandardScaler(),OneClassSVM(gamma=1,nu=.2)).fit(x)print(bool(m.decision_function([[20]])[0]<0))Expected output (locally tested): True. A distant point is outside this fitted reference boundary.
Common mistakes
Do not fit scaling on the holdout, interpret nu as future false-positive rate, or silently replace zero MAD with a tiny constant without discussing consequences.
Practice and next steps
Compute median and MAD for [0,1,1,2,100]. Compare a future residual 4 with a tolerance of three MAD units. Discuss when a zero MAD reflects quantization.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
One-class SVM and residuals — knowledge check
Apply one-class svm and residuals to small examples and debugging scenarios.
Why scale RBF features?
What is MAD?
What if MAD is zero?
What does nu not guarantee?
What can a univariate residual miss?