Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

OneClassSVM

OneClassSVM is an unsupervised estimator for detecting outliers by estimating the support of a distribution. It uses kernel methods and labels samples as inliers or outliers.

How it works

OneClassSVM learns a boundary around the support of a distribution without target labels. It uses a kernel-based libsvm implementation; after fitting, its decision function and prediction classify observations relative to that learned support, returning 1 for inliers and -1 for outliers.

Syntax

class sklearn.svm.OneClassSVM(*, kernel='rbf', degree=3, gamma='scale', coef0=0.0, tol=0.001, nu=0.5, shrinking=True, cache_size=200, verbose=False, max_iter=-1)

Parameters

NameTypeDescription
kernel{'linear', 'poly', 'rbf', 'sigmoid', 'precomputed'} or callableKernel type: linear, poly, rbf, sigmoid, precomputed, or a callable; default is rbf.
degreeintPolynomial kernel degree; default 3 and must be non-negative.
gamma{'scale', 'auto'} or floatKernel coefficient; default is scale.
coef0floatIndependent term in the kernel function; default 0.0.
tolfloatTolerance for the stopping criterion; default 0.001.
nufloatControls the proportion of training errors and support vectors; default 0.5.
shrinkingboolWhether to use the shrinking heuristic; default True.
cache_sizefloatKernel-cache size in megabytes; default 200.
verboseboolEnable verbose output; default False.
max_iterintMaximum iterations, or -1 for no limit; default -1.

Returns

predict returns an array containing +1 or -1 for each sample; fit_predict fits on X and returns those labels.

Key points

  • The estimator is designed for detecting unusual observations without supervised target labels.
  • Its learned boundary represents the region of feature space regarded as supported by the data.
  • The kernel parameter selects the similarity transformation used by the model.
  • A custom callable kernel can supply the kernel matrix before optimization.
  • The prediction output uses 1 for inliers and -1 for outliers.

Common mistakes

  • Its behavior and computational characteristics come from libsvm, so kernel choice and the available libsvm parameters affect the learned boundary.
  • The polynomial-kernel degree cannot be negative.
  • Predictions are binary inlier/outlier labels, not class names or probabilities.

Example: Detect outliers with OneClassSVM

from sklearn.svm import OneClassSVM

X = [[0.0], [0.44], [0.45], [0.46], [1.0]]
model = OneClassSVM(gamma="auto").fit(X)
labels = model.predict(X)
print(labels)

Fits an unsupervised model to one-dimensional observations and labels each observation as an inlier (1) or outlier (-1).

When to use it

Unsupervised anomaly detection

You have mostly normal observations and need to flag unusual records without labeled anomalies.

Kernel-based novelty detection

You need a nonlinear boundary around observations in feature space.

Novelty screening

You want to classify future observations as similar to or different from a training distribution.

Interview questions

QWhat problem does OneClassSVM solve?

OneClassSVM is an unsupervised outlier-detection estimator that estimates the support of a high-dimensional distribution. It is trained without target labels and uses a kernel-based libsvm implementation.

Follow-up: How would you interpret a prediction of -1? It identifies the observation as outside the learned support; +1 identifies it as inside.

QWhich kernels can OneClassSVM use?

The default kernel is RBF, while linear, polynomial, sigmoid, precomputed, and callable kernels are also supported. A callable is used to precompute the kernel matrix.

Follow-up: What parameter controls polynomial-kernel degree? The degree parameter does, and it must be non-negative.

QHow are fitted observations classified?

After fitting, predict returns +1 or -1 for each observation. score_samples provides scores, while decision_function provides the shifted decision values used to assess position relative to the boundary.

Follow-up: How could you obtain labels directly while fitting? Use fit_predict, which performs fitting and returns labels for X.

  • sklearn.svm
  • SVC
  • SVR
  • NuSVC
  • LinearSVC
  • fit_predict
  • decision_function
  • score_samples

Source: OneClassSVM — scikit-learn 1.9.1 documentation. Checked 2026-10-15.