Categories &

Functions List

Function Reference: lof

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

Detect anomalies with the Local Outlier Factor (LOF) method.

Mdl = lof (X) fits a Local Outlier Factor model to the N-by-P matrix X, whose rows are observations and columns are variables, and returns a LocalOutlierFactor object Mdl.

[Mdl, tf, scores] = lof (X) also returns the N-by-1 logical vector tf flagging the anomalous observations and the N-by-1 vector scores of LOF values. A score near 1 indicates an inlier, whereas a score well above 1 indicates an outlier lying in a region sparser than its neighbors.

The Local Outlier Factor of an observation is the average ratio of the local reachability density of its NumNeighbors nearest neighbors to its own local reachability density, where the local reachability density is the inverse mean reachability distance to those neighbors and the reachability distance from p to o is max (k-distance (o), d (p, o)).

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

NameValue
'NumNeighbors'the number of nearest neighbors, a positive integer less than N. The default is min (20, u - 1), where u is the number of unique observations.
'Distance'the distance metric used to find neighbors, one of the metrics accepted by pdist2 ('euclidean' by default).
'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.
'Exponent'the Minkowski distance exponent (default 2), used only with the 'minkowski' distance.
'Cov'the covariance matrix used only with the 'mahalanobis' distance.

Source Code: lof

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

See also: LocalOutlierFactor, isanomaly, dbscan, robustcov

Source Code: lof

Flag a handful of outliers around a Gaussian cluster.

 X = [randn(100,2); 4 + randn(6,2)];
 [Mdl, tf, scores] = lof (X, "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 ("lof: inliers vs. flagged anomalies");