Skip to content
AI360XpertBeta

One-Class SVM

Learn one closed wall around normal examples only, then flag anything outside it as novel without ever seeing what abnormal looks like.

A closed boundary learned from normal points only accepts an inside point and flags an outside one, with nu 0.10 catching 0.90 at 0.07 false cost
A closed boundary learned from normal points only accepts an inside point and flags an outside one, with nu 0.10 catching 0.90 at 0.07 false cost

Why Does This Exist?

A parts line holds millions of passing examples and almost no recorded failures, since each failure mode is rare and the next one has not happened yet. Two-class separation needs both sides labeled, covered in support vector machines, and there is no failure side to train on.

One-class SVM learns a closed wall around normal only and flags whatever falls outside as novel. Novelty detection assumes training rows are clean and normal, unlike anomaly hunting inside dirty data. This page covers the wall, the nu knob, and its price. The scoring frame around it lives in anomaly detection. The tree-based alternative lives in isolation forest. The margin machinery it borrows is explained in kernel trick.

Grounding first: ν\nu (nu) means the allowed fraction of training points outside the wall, a number between 0 and 1. Support vectors mean the boundary-defining points that hold the wall in place.

Think of It Like This

A bouncer who knows the regulars

A venue bouncer has worked one club for years and knows the regular crowd by face, arrival time, and usual headcount. Nobody handed over a photo book of troublemakers, since trouble is rare and each case looks different. What the bouncer learned is the shape of a normal night and how much variation stays ordinary.

Someone far outside that shape draws a second look, not by matching a bad template but by missing the normal one. The regulars are the training cloud, the sense of an ordinary night is the wall, and the second look is the flag. Where it stops: a bouncer can ask questions and update the picture in one glance, while the wall is fixed after fitting and needs retraining plus a chosen nu to move.

How It Actually Works

One wall from one side

Two-class machines maximize the gap between labeled sides. One-class machines keep the same margin idea with one side: enclose most normal points inside the smallest wall that still leaves room for variation. A radial basis kernel bends that wall around curved clusters that no ellipse fits, at the price of a width setting alongside nu.

What nu prices

Nu caps the training fraction allowed outside and floors the fraction that become support vectors. Small nu builds a wide forgiving wall, large nu builds a tight strict one. Widening drops false alarms on normal data and lets more real novelties through. Tightening catches more novelties and flags more normal rows too. No setting improves both sides at once, so the choice prices errors rather than discovering truth.

Worked example: three nu settings, one testbed

Train on 500500 normal rows in two dimensions, no anomalies. Score 200200 fresh normal rows plus 2020 uniform novelties across a wide square. False-positive rate means flagged share of the 200200 normal rows. Caught rate means flagged share of the 2020 novelties.

nu 0.01: false 12/200=0.06, caught 15/20=0.75\text{nu } 0.01: \text{ false } 12/200 = 0.06, \quad \text{ caught } 15/20 = 0.75 nu 0.10: false 14/200=0.07, caught 18/20=0.90\text{nu } 0.10: \text{ false } 14/200 = 0.07, \quad \text{ caught } 18/20 = 0.90 nu 0.40: false 76/200=0.38, caught 19/20=0.95\text{nu } 0.40: \text{ false } 76/200 = 0.38, \quad \text{ caught } 19/20 = 0.95

Moving from 0.01 to 0.10 gains 0.150.15 caught for 0.010.01 extra false cost. Moving from 0.10 to 0.40 gains 0.050.05 caught for 0.310.31 extra false cost. Those three pairs sit in the diagram strip: wide, balanced, and tight but noisy. The middle setting is the usual starting point, not a default answer.

Code

The point is the check: sweep nu on identical normal training data and confirm detection rises with false alarms.

import numpy as npfrom sklearn.svm import OneClassSVM
rng = np.random.default_rng(5)train = rng.normal(0.0, 1.0, (500, 2))test_normal = rng.normal(0.0, 1.0, (200, 2))test_novel = rng.uniform(-6.0, 6.0, (20, 2))
for nu in (0.01, 0.10, 0.40):    wall = OneClassSVM(kernel="rbf", gamma="scale", nu=nu).fit(train)    false_rate: float = float((wall.predict(test_normal) == -1).mean())    caught_rate: float = float((wall.predict(test_novel) == -1).mean())    print(f"nu={nu:.2f} false {false_rate:.2f} caught {caught_rate:.2f}")# -> nu=0.01 false 0.06 caught 0.75# -> nu=0.10 false 0.07 caught 0.90# -> nu=0.40 false 0.38 caught 0.95

The printout matches the hand fractions: 12 of 200, 14 of 200, and 76 of 200 against 15, 18, and 19 of 20. Change gamma from scale to 0.1 and watch the wall tighten independently of nu, which is why both knobs need joint checking.

Watch Out For

Training on rows that already contain the target

The wall assumes clean normal input, so a contaminated batch teaches the boundary to swallow the very cases it should flag. Even five percent mixed anomalies widen the wall past usefulness.

Symptom: training outside-fraction far above nu, plus missed novelties that resemble the contamination cluster. Fix: clean the training set first, and when cleanliness cannot be promised use a contamination-tolerant scorer such as isolation forest with its own contamination setting.

Setting nu from false alarms alone

Teams pick the largest nu whose false rate still looks tolerable and never check what it catches. A quiet wall can catch almost nothing while looking well tuned.

Symptom: false rate 0.02 with caught rate 0.40, so the system pages rarely and misses mostly. Fix: report both rates for every candidate nu, price them against the cost of a miss versus a false page, and choose like any cutoff in threshold selection.

The Quick Version

  • One-class SVM fits one closed wall around normal-only training data and flags the outside as novel.
  • A radial basis kernel bends the wall around curved normal clusters that ellipses cannot follow.
  • Nu 0.01 gives false 0.06 with caught 0.75 on our testbed of 200 normal rows and 20 novelties.
  • Nu 0.10 gives false 0.07 with caught 0.90, the balanced starting point.
  • Nu 0.40 gives false 0.38 with caught 0.95, so the last gains cost far more false pages.
  • Training rows must be clean normal data, and every nu needs both rates reported before it ships.