-
Book Overview & Buying
-
Table Of Contents
scikit-learn Cookbook - Third Edition
By :
LOF is a density-based anomaly detection method that identifies outliers by comparing the local density of each data point to that of its neighbors. Rather than using a global threshold, LOF assesses how isolated a data point is with respect to the surrounding neighborhood. If a point lies in a region of significantly lower density than its neighbors, it is flagged as an outlier.
LOF is especially effective in datasets where the density of data points varies across the feature space. It can detect local anomalies that may be overlooked by global methods like Isolation Forest.
We’ll generate a dataset containing clusters with different densities and add noise to simulate outliers.
Load the libraries:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.neighbors import LocalOutlierFactorCreate synthetic data with clusters and noise:
X, _ = make_blobs(n_samples=400, centers=[[0, 0], [5...