Training-data outlier detection
Clean a dataset before downstream modeling by identifying samples that differ strongly from nearby observations.
Scikit-Learn · Class
LocalOutlierFactor is an unsupervised estimator for detecting observations whose local density is substantially lower than that of their nearest neighbors. It uses k-nearest-neighbor distances to compute local outlier scores.
LOF estimates each sample’s local density from its nearest neighbors, then compares that density with neighboring densities. Samples that are substantially less dense than their surroundings receive stronger abnormality scores and can be labeled as outliers.
class sklearn.neighbors.LocalOutlierFactor(*, n_neighbors=20, algorithm='auto', leaf_size=30, metric='minkowski', p=2, metric_params=None, contamination='auto', novelty=False, n_jobs=None)| Name | Type | Description |
|---|---|---|
n_neighbors | int, default=20 | Number of neighbors used for the LOF calculation. |
algorithm | {'auto', 'ball_tree', 'kd_tree', 'brute'}, default='auto' | Algorithm used to compute nearest neighbors. |
leaf_size | int, default=30 | Leaf size passed to the tree-based neighbor search. |
metric | str or callable, default='minkowski' | Distance metric used for neighbor searches. |
p | float, default=2 | Power parameter for the Minkowski metric. |
metric_params | dict, default=None | Additional keyword arguments for the metric function. |
contamination | 'auto' or float, default='auto' | Expected proportion of outliers, or 'auto'. |
novelty | bool, default=False | Enables novelty detection for unseen data when set before fitting. |
n_jobs | int, default=None | Number of parallel jobs used for neighbor search; None means one job. |
A fitted LocalOutlierFactor estimator; fit_predict returns labels for the input samples, and negative_outlier_factor_ stores training-sample abnormality scores.
from sklearn.neighbors import LocalOutlierFactor
X = [[1.0, 1.0], [1.1, 1.0], [0.9, 1.1], [1.0, 0.9], [8.0, 8.0]]
lof = LocalOutlierFactor(n_neighbors=3, contamination="auto")
labels = lof.fit_predict(X)
print(labels) # -1 denotes an outlier; 1 denotes an inlierFits LOF for outlier detection and returns a label for every training sample.
Clean a dataset before downstream modeling by identifying samples that differ strongly from nearby observations.
Find anomalies in data with clusters of different densities, where a global distance threshold would be inadequate.
Score or label future observations against a fitted reference set.
LOF is an unsupervised outlier detector that compares a sample’s local density with the densities around it. A sample is treated as anomalous when its density is substantially lower than that of its neighbors.
Follow-up: How does LOF define locality? It uses k-nearest neighbors, with distances estimating local density.
The default workflow is to call fit_predict on the data being examined. The result labels inliers and outliers, while negative_outlier_factor_ exposes abnormality scores for training samples.
Follow-up: How would you inspect new data? Construct the estimator with novelty=True before fitting, then use its prediction and scoring methods.
n_neighbors controls the neighborhood used for the density comparison. A value of 20 is the documented default, while the appropriate value depends on the local cluster structure.
Follow-up: What trade-off does changing it create? Smaller neighborhoods emphasize finer local structure; larger neighborhoods produce a broader density comparison.
NearestNeighborsKNeighborsClassifierKNeighborsRegressoroutlier_detection