Categories &

Functions List

Function Reference: ocsvm

statistics: Mdl = ocsvm (X)
statistics: [Mdl, tf] = ocsvm (X)
statistics: [Mdl, tf, scores] = ocsvm (X)
statistics: […] = ocsvm (…, name, value)

Detect anomalies with a one-class support vector machine.

Mdl = ocsvm (X) fits a one-class support vector machine to the N-by-P matrix X, whose rows are observations and columns are variables, and returns a OneClassSVM object Mdl.

[Mdl, tf, scores] = ocsvm (X) also returns the N-by-1 logical vector tf flagging the anomalous observations and the N-by-1 vector scores of anomaly scores. A higher score indicates an observation that lies further outside the boundary enclosing the data, and is therefore more likely to be an anomaly.

The observations are mapped to a randomized feature space that approximates a Gaussian kernel of scale KernelScale using NumExpansionDimensions features, and a linear one-class boundary is fitted there with ridge regularization of strength Lambda.

Additional parameters can be specified by Name-Value pair arguments.

NameValue
'KernelScale'the scale of the approximated Gaussian kernel, a positive scalar or 'auto' (default).
'Lambda'the ridge regularization strength, a nonnegative scalar or 'auto' (default).
'NumExpansionDimensions'the number of expanded feature dimensions, a positive integer or 'auto' (default).
'StandardizeData'a logical scalar (default false); when true each predictor is centered and scaled and the means and standard deviations are stored in Mdl.Mu and Mdl.Sigma.
'ContaminationFraction'the assumed fraction of anomalies in X, a scalar in [0, 1] (default 0). It sets Mdl.ScoreThreshold to quantile (scores, 1 - ContaminationFraction); when it is 0 the threshold is the maximum score and no training observation is flagged.

Source Code: ocsvm

The feature expansion uses random projections, so the scores depend on the state of the random number generator and are not reproducible across runs unless the generator is seeded. The 'auto' selections and the fitted model differ from MATLAB’s implementation, which uses a different feature expansion and solver.

For a deterministic, classic one-class support vector machine (a nu-SVM with an exact kernel, computed through libsvm), use fitcsvm with a single class in the response or with the 'Nu' name-value argument; that path returns a ClassificationSVM object whose predict method labels observations, rather than the anomaly-scoring interface provided here.

Use the isanomaly method of Mdl to detect anomalies in new data.

See also: OneClassSVM, isanomaly, iforest, lof, fitcsvm

Source Code: ocsvm

Flag a handful of outliers around a Gaussian cluster.

 X = [randn(200,2); 5 + randn(10,2)];
 [Mdl, tf, scores] = ocsvm (X, "KernelScale", 2, ...
                            "ContaminationFraction", 0.05);
plotted figure

 gscatter (X(:,1), X(:,2), tf);
error: Invalid call to legend.  Correct usage is:

 -- legend ()
 -- legend COMMAND
 -- legend (STR1, STR2, ...)
 -- legend (CHARMAT)
 -- legend ({CELLSTR})
 -- legend (..., PROPERTY, VALUE, ...)
 -- legend (HOBJS, ...)
 -- legend ("COMMAND")
 -- legend (HAX, ...)
 -- legend (HLEG, ...)
 -- HLEG = legend (...)
 title ("ocsvm: inliers vs. flagged anomalies");