.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/neighbors/plot_lof_outlier_detection.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_neighbors_plot_lof_outlier_detection.py: ================================================= Outlier detection with Local Outlier Factor (LOF) ================================================= The Local Outlier Factor (LOF) algorithm is an unsupervised anomaly detection method which computes the local density deviation of a given data point with respect to its neighbors. It considers as outliers the samples that have a substantially lower density than their neighbors. This example shows how to use LOF for outlier detection which is the default use case of this estimator in scikit-learn. Note that when LOF is used for outlier detection it has no `predict`, `decision_function` and `score_samples` methods. See the :ref:`User Guide ` for details on the difference between outlier detection and novelty detection and how to use LOF for novelty detection. The number of neighbors considered (parameter `n_neighbors`) is typically set 1) greater than the minimum number of samples a cluster has to contain, so that other samples can be local outliers relative to this cluster, and 2) smaller than the maximum number of close by samples that can potentially be local outliers. In practice, such information is generally not available, and taking `n_neighbors=20` appears to work well in general. .. GENERATED FROM PYTHON SOURCE LINES 24-28 .. code-block:: Python # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 29-31 Generate data with outliers --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 33-46 .. code-block:: Python import numpy as np np.random.seed(42) X_inliers = 0.3 * np.random.randn(100, 2) X_inliers = np.r_[X_inliers + 2, X_inliers - 2] X_outliers = np.random.uniform(low=-4, high=4, size=(20, 2)) X = np.r_[X_inliers, X_outliers] n_outliers = len(X_outliers) ground_truth = np.ones(len(X), dtype=int) ground_truth[-n_outliers:] = -1 .. GENERATED FROM PYTHON SOURCE LINES 47-53 Fit the model for outlier detection (default) --------------------------------------------- Use `fit_predict` to compute the predicted labels of the training samples (when LOF is used for outlier detection, the estimator has no `predict`, `decision_function` and `score_samples` methods). .. GENERATED FROM PYTHON SOURCE LINES 53-61 .. code-block:: Python from sklearn.neighbors import LocalOutlierFactor clf = LocalOutlierFactor(n_neighbors=20, contamination=0.1) y_pred = clf.fit_predict(X) n_errors = (y_pred != ground_truth).sum() X_scores = clf.negative_outlier_factor_ .. GENERATED FROM PYTHON SOURCE LINES 62-64 Plot results ------------ .. GENERATED FROM PYTHON SOURCE LINES 66-96 .. code-block:: Python import matplotlib.pyplot as plt from matplotlib.legend_handler import HandlerPathCollection def update_legend_marker_size(handle, orig): "Customize size of the legend marker" handle.update_from(orig) handle.set_sizes([20]) plt.scatter(X[:, 0], X[:, 1], color="k", s=3.0, label="Data points") # plot circles with radius proportional to the outlier scores radius = (X_scores.max() - X_scores) / (X_scores.max() - X_scores.min()) scatter = plt.scatter( X[:, 0], X[:, 1], s=1000 * radius, edgecolors="r", facecolors="none", label="Outlier scores", ) plt.axis("tight") plt.xlim((-5, 5)) plt.ylim((-5, 5)) plt.xlabel("prediction errors: %d" % (n_errors)) plt.legend( handler_map={scatter: HandlerPathCollection(update_func=update_legend_marker_size)} ) plt.title("Local Outlier Factor (LOF)") plt.show() .. image-sg:: /auto_examples/neighbors/images/sphx_glr_plot_lof_outlier_detection_001.png :alt: Local Outlier Factor (LOF) :srcset: /auto_examples/neighbors/images/sphx_glr_plot_lof_outlier_detection_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.081 seconds) .. _sphx_glr_download_auto_examples_neighbors_plot_lof_outlier_detection.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/1.6.X?urlpath=lab/tree/notebooks/auto_examples/neighbors/plot_lof_outlier_detection.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/neighbors/plot_lof_outlier_detection.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_lof_outlier_detection.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_lof_outlier_detection.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_lof_outlier_detection.zip ` .. include:: plot_lof_outlier_detection.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_