Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

LocalOutlierFactor

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.

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
n_neighborsint, default=20Number of neighbors used for the LOF calculation.
algorithm{'auto', 'ball_tree', 'kd_tree', 'brute'}, default='auto'Algorithm used to compute nearest neighbors.
leaf_sizeint, default=30Leaf size passed to the tree-based neighbor search.
metricstr or callable, default='minkowski'Distance metric used for neighbor searches.
pfloat, default=2Power parameter for the Minkowski metric.
metric_paramsdict, default=NoneAdditional keyword arguments for the metric function.
contamination'auto' or float, default='auto'Expected proportion of outliers, or 'auto'.
noveltybool, default=FalseEnables novelty detection for unseen data when set before fitting.
n_jobsint, default=NoneNumber of parallel jobs used for neighbor search; None means one job.

Returns

A fitted LocalOutlierFactor estimator; fit_predict returns labels for the input samples, and negative_outlier_factor_ stores training-sample abnormality scores.

Key points

  • This class detects unusual observations without requiring labeled training targets.
  • Each sample receives a LOF-based abnormality score.
  • The method compares a point’s density with the density of nearby points.
  • Neighborhood size and distance determine the local density estimate.
  • Outlier detection is based on a relative, rather than global, density comparison.
  • Samples substantially less dense than their neighbors are identified as outliers.

Example: Detect training-set outliers

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 inlier

Fits LOF for outlier detection and returns a label for every training sample.

When to use it

Training-data outlier detection

Clean a dataset before downstream modeling by identifying samples that differ strongly from nearby observations.

Local-density anomaly detection

Find anomalies in data with clusters of different densities, where a global distance threshold would be inadequate.

Novelty detection

Score or label future observations against a fitted reference set.

Interview questions

QWhat does LocalOutlierFactor measure?

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.

QHow is LOF used for ordinary outlier detection?

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.

QWhat role does n_neighbors play?

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.

  • NearestNeighbors
  • KNeighborsClassifier
  • KNeighborsRegressor
  • outlier_detection

Source: LocalOutlierFactor — scikit-learn 1.9.1 documentation. Checked 2026-10-15.