ocsvm
Fit one-class support vector machine (SVM) model for anomaly detection
Since R2022b
Syntax
Description
Use the ocsvm
function to fit a one-class support vector
machine (SVM) model for outlier detection and novelty detection.
Outlier detection (detecting anomalies in training data) — Use the output argument
tf
ofocsvm
to identify anomalies in training data.Novelty detection (detecting anomalies in new data with uncontaminated training data) — Create a
OneClassSVM
object by passing uncontaminated training data (data with no outliers) toocsvm
. Detect anomalies in new data by passing the object and the new data to the object functionisanomaly
.
returns
a Mdl
= ocsvm(Tbl
)OneClassSVM
object
(one-class SVM model object) for predictor data in the table
Tbl
.
specifies options using one or more name-value arguments in addition to any of the input
argument combinations in the previous syntaxes. For example,
Mdl
= ocsvm(___,Name=Value
)
instructs the function
to process 10% of the training data as anomalies.ContaminationFraction
=0.1
Examples
Input Arguments
Output Arguments
More About
Tips
After training a model, you can generate C/C++ code that finds anomalies for new data. Generating C/C++ code requires MATLAB® Coder™. For details, see Code Generation of the
isanomaly
function and Introduction to Code Generation.
Algorithms
ocsvm
considersNaN
,''
(empty character vector),""
(empty string),<missing>
, and<undefined>
values inTbl
andNaN
values inX
to be missing values.ocsvm
does not use observations with some missing values. The function assigns the anomaly score ofNaN
and anomaly indicator offalse
(logical0
) to the observations.ocsvm
minimizes the regularized objective function using a Limited-memory Broyden-Fletcher-Goldfarb-Shanno (LBFGS) solver with ridge (L2) regularization. Ifocsvm
requires more memory than the value ofBlockSize
to hold the transformed predictor data, then the function uses a block-wise strategy.When
ocsvm
uses a block-wise strategy, it implements LBFGS by distributing the calculation of the loss and gradient among different parts of the data at each iteration. Also,ocsvm
refines the initial estimates of the linear coefficients and the bias term by fitting the model locally to parts of the data and combining the coefficients by averaging. If you specifyVerbose=1
, thenocsvm
displays diagnostic information for each data pass.When
ocsvm
does not use a block-wise strategy, the initial estimates are zeros. If you specifyVerbose=1
, thenocsvm
displays diagnostic information for each iteration.
Alternative Functionality
You can also use the fitcsvm
function to train a one-class SVM model for
anomaly detection.
The
ocsvm
function provides a simpler and preferred workflow for anomaly detection than thefitcsvm
function.The
ocsvm
function returns aOneClassSVM
object, anomaly indicators, and anomaly scores. You can use the outputs to identify anomalies in training data. To find anomalies in new data, you can use theisanomaly
object function ofOneClassSVM
. Theisanomaly
function returns anomaly indicators and scores for the new data.The
fitcsvm
function supports both one-class and binary classification. If the class label variable contains only one class (for example, a vector of ones),fitcsvm
trains a model for one-class classification and returns aClassificationSVM
object. To identify anomalies, you must first compute anomaly scores by using theresubPredict
orpredict
object function ofClassificationSVM
, and then identify anomalies by finding observations that have negative scores.Note that a large positive anomaly score indicates an anomaly in
ocsvm
, whereas a negative score indicates an anomaly inpredict
ofClassificationSVM
.
The
ocsvm
function finds the decision boundary based on the primal form of SVM, whereas thefitcsvm
function finds the decision boundary based on the dual form of SVM.The solver in
ocsvm
is computationally less expensive than the solver infitcsvm
for a large data set (large n). Unlike solvers infitcsvm
, which require computation of the n-by-n Gram matrix, the solver inocsvm
only needs to form a matrix of size n-by-m. Here, m is the number of dimensions of expanded space, which is typically much less than n for big data.
References
Version History
Introduced in R2022b