Unsupervised anomaly detection
You have mostly normal observations and need to flag unusual records without labeled anomalies.
Scikit-Learn · Class
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.
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.
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)| Name | Type | Description |
|---|---|---|
kernel | {'linear', 'poly', 'rbf', 'sigmoid', 'precomputed'} or callable | Kernel type: linear, poly, rbf, sigmoid, precomputed, or a callable; default is rbf. |
degree | int | Polynomial kernel degree; default 3 and must be non-negative. |
gamma | {'scale', 'auto'} or float | Kernel coefficient; default is scale. |
coef0 | float | Independent term in the kernel function; default 0.0. |
tol | float | Tolerance for the stopping criterion; default 0.001. |
nu | float | Controls the proportion of training errors and support vectors; default 0.5. |
shrinking | bool | Whether to use the shrinking heuristic; default True. |
cache_size | float | Kernel-cache size in megabytes; default 200. |
verbose | bool | Enable verbose output; default False. |
max_iter | int | Maximum iterations, or -1 for no limit; default -1. |
predict returns an array containing +1 or -1 for each sample; fit_predict fits on X and returns those labels.
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).
You have mostly normal observations and need to flag unusual records without labeled anomalies.
You need a nonlinear boundary around observations in feature space.
You want to classify future observations as similar to or different from a training distribution.
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.
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.
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.svmSVCSVRNuSVCLinearSVCfit_predictdecision_functionscore_samples