LocalOutlierFactor
#include <Skigen/Neighbors>
template <typename Scalar = double>
class Skigen::LocalOutlierFactor(n_neighbors=20, contamination=-1)
Unsupervised Outlier Detection using the Local Outlier Factor.
The anomaly score of each sample is called the Local Outlier Factor. It measures the local deviation of the density of a given sample with respect to its neighbors. It is local in that the anomaly score depends on how isolated the object is with respect to the surrounding neighborhood. More precisely, locality is given by k-nearest neighbors, whose distance is used to estimate the local density.
By comparing the local density of a sample to the local densities of its neighbors, one can identify samples that have a substantially lower density than their neighbors. These are considered outliers.
Mirrors sklearn.neighbors.LocalOutlierFactor.
Parameters:
-
n_neighbors : int, default=20 Number of neighbors to use (
int, default20). -
contamination : Scalar, default=-1 Proportion of outliers (
Scalar, default-1meaning no automatic threshold).
Attributes:
-
n_neighbors : int Number of neighbors requested.
-
n_neighbors_used : int Effective number of neighbors used (clamped to n_samples − 1).
-
negative_outlier_factor : VectorType Negative LOF scores of the training samples (n_samples,).
-
offset : Scalar Threshold for labeling outliers.
-
fit_predict_labels : Eigen::VectorXi Predict outlier labels for the training data.
-
lof_scores : VectorType Return the raw (positive) LOF scores for the training data.
Methods
SKIGEN_PARAMS()
Fit the LOF model from the training data.
Computes the LOF score for every training sample.
Parameters:
- X Training data of shape (n_samples, n_features).
Returns:
- result
Reference to the fitted estimator (
*this).
Throws:
std::invalid_argument— if n_samples < 2.
Example
Skigen::LocalOutlierFactor<double> lof(20);
lof.fit(X);
auto scores = lof.negative_outlier_factor();
auto labels = lof.fit_predict_labels();
std::cout << "=== Local Outlier Factor ===\n"
<< "Neighbors used: " << lof.n_neighbors_used() << "\n"
<< "Offset: " << lof.offset() << "\n\n";