Clustering, density estimation, and anomaly detection · Anomaly detection · UCL056 · Algorithm
One-class support vector machine
Learn a boundary around a reference distribution in kernel feature space, often from predominantly normal training examples, then flag observations falling outside that boundary.
In CodeSplash
A CodeSplash method runs this entry in Python and R. Set it up from the method's page.
- One-class SVM Verified in Python and R
Do it in CodeSplash Run it on your own data, in Python or R.
- Availability
- Runs in CodeSplash
- Code example
- Component demo, not a trained model
- Who ran it
- CodeSplash's run and the reference's record
- CodeSplash's run
- Verified in Python and R through discover.ocsvm. The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Learn a boundary around a reference distribution in kernel feature space, often from predominantly normal training examples, then flag observations falling outside that boundary.
Question / hypotheses
Which observations deviate from the chosen reference notion of normality, and at what operational alert rate? This entry defines an estimation or optimization objective, not an intrinsic null/alternative hypothesis test.
Architecture / computational structure
Kernel feature mapping and a regularized separating hyperplane in that space.
Assumptions
Under the optimization assumptions, bounds training-error and support-vector fractions in specified directions; it is not an exact future anomaly rate. RBF bandwidth and interact strongly. Training contamination can distort the accepted region, and kernel methods can be costly at large sample sizes.
Formula / computational definition
Minimize subject to and . The signed decision is . Kernel evaluations replace explicit feature vectors.
Parameters and notation
Learned or fitted quantities: Support coefficients, offset, and resulting boundary. User-set hyperparameters/design choices: Choose kernel, kernel bandwidth, , scaling, and solver tolerance. Tune these against the stated objective and stability/validation criteria; do not select settings on the final evaluation set.
Fitting, fine-tuning, and adaptation
Neural fine-tuning is not applicable to this classical detector. Refit the reference model when its population changes, then recalibrate an alert threshold on a separate normal holdout. Tune sensitivity without using final-test anomaly labels repeatedly.
How to use and calibrate
Calibrate the operating threshold on separate normal reference data and evaluate unseen anomalies if available. Plot acceptance regions in simple cases and test sensitivity to scaling and bandwidth.
Example application
Learn a nonlinear normality boundary from a reference cohort. Define the reference population, measurement units, and success criterion before fitting; investigate whether discovered groups or alerts reflect the intended phenomenon.
Interpretation / pitfalls
Watch out
The reported result is conditional on the representation and objective. An alert is a screening result, not a diagnosis or a calibrated posterior probability. Evaluate both ranking and the decision threshold.
Python and R code examples
Use sklearn.svm.OneClassSVM; R e1071::svm(type='one-classification') provides a native route. Score signs and default scaling differ, so make preprocessing and score orientation explicit before comparing results.
Component: Kernel one-class decision function for supplied support weights. Python: executed successfully; R: not executed (Rscript unavailable). These small fixtures establish the arithmetic operation, not full-method accuracy or reproduction of paper results.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Component demo, not a trained model — Kernel one-class decision function for supplied support weights. · needs numpy
import numpy as np
x=np.array([-2.,0.,2.]); support=np.array([-1.,1.]); alpha=np.array([.5,.5]); gamma=.5; rho=.4
K=np.exp(-gamma*(x[:,None]-support)**2); result=K@alpha-rho
print(result)
Component demo, not a trained model — Kernel one-class decision function for supplied support weights.
x=c(-2,0,2); support=c(-1,1); alpha=c(.5,.5); gamma=.5; rho=.4
K=exp(-gamma*outer(x,support,'-')^2); result=as.vector(K%*%alpha)-rho
print(result)
Sources
- Source 1scikit-learn.org
- Source 2cran.r-project.org
From the CodeSplash reference, entry UCL056.