One-class SVM

Also known as: one-class support vector machine

On this page 7

A one-class SVM (one-class support vector machine) is a kernel method that learns a boundary enclosing the normal data from examples of a single class and labels new points outside that boundary as anomalies. In anomaly detection, it is the classic model for novelty detection: it is trained on data assumed to be clean and then scores unseen observations by how far they fall from the learned region.

The method was introduced by Schölkopf and colleagues around 2001. It remains a standard baseline in libraries such as scikit-learn and PyOD because it handles nonlinear, multimodal normal behavior with a small number of support vectors and a well-understood optimization problem.

How a one-class SVM separates normal data from the origin

The one-class SVM maps each input through a kernel feature map into a high-dimensional space and then finds a hyperplane that separates the data from the origin with maximum margin. Points on the data side of the hyperplane are considered normal; points on the origin side are flagged as outliers.

Training solves a convex quadratic program. It minimizes the squared norm of the weight vector, minus an offset called rho, plus a penalty on slack variables scaled by 1 divided by nu times n. The solution depends only on a subset of training points, the support vectors, so the decision function is a weighted sum of kernel evaluations against them.

The resulting decision function returns a signed score: positive inside the learned region and negative outside. Its magnitude acts as an anomaly score, although it is not a calibrated probability. The main components are summarized below.

ComponentRolePractical note
Kernel functionDefines similarity and the shape of the boundaryRBF is the usual default
Support vectorsTraining points that define the boundaryTheir count grows with nu
Offset rhoPosition of the separating hyperplaneLearned during training
Decision functionSigned distance to the boundaryThreshold at zero by default
Slack variablesAllow some training points outsideControl robustness to contamination
Components of the one-class SVM model

The nu and gamma parameters

Two hyperparameters dominate the behavior of a one-class SVM with an RBF kernel. The parameter nu lies between 0 and 1 and has a precise interpretation: it is an upper bound on the fraction of training outliers and a lower bound on the fraction of support vectors. Setting nu to 0.05 therefore lets about 5 percent of training points fall outside the boundary.

The kernel width gamma controls how local the boundary is. A large gamma produces tight, wiggly regions around individual points, while a small gamma gives a smooth, almost elliptical envelope. The table describes typical effects.

SettingBoundary shapeTypical risk
Low nu (0.01)Encloses almost all training pointsMisses anomalies hidden in training data
High nu (0.2)Excludes a larger share of pointsMany false alarms on normal data
Low gammaSmooth and broad regionUnderfits multimodal normal behavior
High gammaTight islands around samplesOverfits and rejects valid new points
Effect of nu and gamma settings

Tuning is difficult because labeled anomalies are usually scarce. Practitioners set nu near the expected contamination rate and choose gamma with heuristics such as 1 divided by the number of features times the feature variance, then validate on a small labeled holdout when one exists.

How it compares with other anomaly detectors

The one-class SVM is closely related to support vector data description, which encloses the data in a minimum-volume hypersphere; with an RBF kernel the two formulations give equivalent solutions. It differs more sharply from tree-based and density-based detectors.

MethodCore ideaScales to large dataMain strength
One-class SVMKernel boundary around normal dataPoorly, about quadratic in samplesFlexible smooth boundaries
Isolation forestAnomalies isolate in few random splitsWellFast with little tuning
Local outlier factorCompares local density with neighborsModeratelyDetects local anomalies
Elliptic envelopeRobust Gaussian covariance fitWellSimple for unimodal data
Deep SVDDNeural network maps data into a hypersphereWell with GPUsHandles images and raw signals
One-class SVM and common alternatives

The one-class SVM suits semi-supervised anomaly detection, where the training set contains only normal behavior. In fully unsupervised settings with unknown contamination, isolation forests are often more robust and much faster.

Practical use and preprocessing

Because the RBF kernel relies on Euclidean distances, feature scaling is mandatory. Standardization or robust scaling prevents one feature with large units from dominating the boundary. Categorical inputs need encoding, and high-dimensional data often benefits from PCA or learned embeddings before training.

  • Industrial monitoring: vibration and temperature features from machines running in a known healthy state.
  • Network security: flow statistics from normal traffic to flag unusual connections.
  • Quality inspection: embeddings from a pretrained vision model of defect-free parts.
  • Fraud screening: behavioral features from verified legitimate customers.

For large datasets, scikit-learn offers SGDOneClassSVM combined with a kernel approximation such as Nystroem features. This linear-time variant trades some boundary precision for the ability to train on millions of samples.

Limitations and common mistakes

The method has known weaknesses that experienced practitioners watch for. Training cost grows between quadratically and cubically with the number of samples, the scores are not probabilities, and performance degrades in very high dimensions where distances lose contrast.

MistakeConsequenceCorrection
Unscaled featuresBoundary driven by one variableStandardize every feature
Contaminated training dataAnomalies absorbed as normalClean data or raise nu slightly
Default gamma on wide dataOverly tight or loose regionTune gamma on a validation set
Treating scores as probabilitiesMisleading alert thresholdsCalibrate thresholds on labeled cases
Training on raw imagesMeaningless pixel distancesUse embeddings or Deep SVDD
Frequent errors with one-class SVM

A frequent misconception is that a one-class SVM learns what anomalies look like. It learns only the shape of normal data, so any genuine change in normal behavior, such as a new product line or a seasonal shift, will be flagged until the model is retrained on updated data.

The short version

A one-class SVM learns what normal looks like. It sees only good examples during training. Then it draws a flexible fence around them.

Anything that lands outside the fence is suspicious. Think of a security guard who knows every regular visitor. A stranger stands out at once, even if the guard has never seen that stranger before. The method works the same way with data.

Where it makes a difference

A manufacturer monitors 40 pumps with accelerometers. Engineers collect three months of vibration data from pumps known to be healthy and compute 24 spectral features per one-minute window. They standardize the features and train a one-class SVM with an RBF kernel and nu set to 0.02.

In operation, each new window receives a decision score. When one pump shows negative scores for six consecutive hours, technicians inspect it and find early bearing wear. The repair happens during a planned stop instead of after a failure, and the model raises only a few false alarms per month across the whole fleet.