.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "checks_gallery/tabular/integrity/plot_outlier_sample_detection.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_checks_gallery_tabular_integrity_plot_outlier_sample_detection.py: Outlier Sample Detection *************** This notebooks provides an overview for using and understanding the Outlier Sample Detection check. **Structure:** * `How deepchecks detects outliers <#How-deepchecks-detects-outliers>`__ * `Prepare data <#prepare-data>`__ * `Run the check <#run-the-check>`__ * `Define a condition <#define-a-condition>`__ How deepchecks detects outliers ======================== Outlier Sample Detection searches for outliers samples (jointly across all features) using the LoOP algorithm. The LoOP algorithm is a robust method for detecting outliers in a dataset across multiple variables by comparing the density in the area of a sample with the densities in the areas of its nearest neighbors (see `link `_ for further details). LoOP relies on a distance matrix. In our implementation we use the Gower distance that averages the distances per feature between samples. For numeric features it calculates the absolute distance divided by the range of the feature and for categorical features it is an indicator whether the values are the same (see `link `_ for further details). .. GENERATED FROM PYTHON SOURCE LINES 29-31 Imports ======= .. GENERATED FROM PYTHON SOURCE LINES 31-39 .. code-block:: default import pandas as pd from sklearn.datasets import load_iris from deepchecks.tabular import Dataset from deepchecks.tabular.checks.integrity.outlier_sample_detection import \ OutlierSampleDetection .. GENERATED FROM PYTHON SOURCE LINES 40-42 Prepare data ========= .. GENERATED FROM PYTHON SOURCE LINES 42-46 .. code-block:: default iris = pd.DataFrame(load_iris().data) iris.describe() .. raw:: html
0 1 2 3
count 150.000000 150.000000 150.000000 150.000000
mean 5.843333 3.057333 3.758000 1.199333
std 0.828066 0.435866 1.765298 0.762238
min 4.300000 2.000000 1.000000 0.100000
25% 5.100000 2.800000 1.600000 0.300000
50% 5.800000 3.000000 4.350000 1.300000
75% 6.400000 3.300000 5.100000 1.800000
max 7.900000 4.400000 6.900000 2.500000


.. GENERATED FROM PYTHON SOURCE LINES 47-48 Add an outlier: .. GENERATED FROM PYTHON SOURCE LINES 48-54 .. code-block:: default outlier_sample = [1, 10, 50, 100] iris.loc[len(iris.index)] = outlier_sample print(iris.tail()) modified_iris = Dataset(iris, cat_features=[]) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 0 1 2 3 146 6.3 2.5 5.0 1.9 147 6.5 3.0 5.2 2.0 148 6.2 3.4 5.4 2.3 149 5.9 3.0 5.1 1.8 150 1.0 10.0 50.0 100.0 .. GENERATED FROM PYTHON SOURCE LINES 55-58 Run the Check ============= We define the nearest_neighbors_percent and the extent parameters for the LoOP algorithm. .. GENERATED FROM PYTHON SOURCE LINES 58-62 .. code-block:: default check = OutlierSampleDetection(nearest_neighbors_percent=0.01, extent_parameter=3) check.run(modified_iris) .. raw:: html

Outlier Sample Detection

Detects outliers in a dataset using the LoOP algorithm.

Additional Outputs
The Outlier Probability Score is calculated by the LoOP algorithm which measures the local deviation of density of a given sample with respect to its neighbors. These outlier scores are directly interpretable as a probability of an object being an outlier (see link for more information).

  Outlier Probability Score 0 1 2 3
150 1.00 1.00 10.00 50.00 100.00
41 0.67 4.50 2.30 1.30 0.30
108 0.50 6.70 2.50 5.80 1.80
109 0.50 7.20 3.60 6.10 2.50
22 0.44 4.60 3.60 1.00 0.20


.. GENERATED FROM PYTHON SOURCE LINES 63-66 Define a condition ================== Now, we define a condition that enforces that the ratio of outlier samples in out dataset is below 0.001. .. GENERATED FROM PYTHON SOURCE LINES 66-69 .. code-block:: default check = OutlierSampleDetection() check.add_condition_outlier_ratio_not_greater_than(max_outliers_ratio=0.001, outlier_score_threshold=0.9) check.run(modified_iris) .. raw:: html

Outlier Sample Detection

Detects outliers in a dataset using the LoOP algorithm.

Conditions Summary
Status Condition More Info
!
Not more than 0.1% of dataset over outlier score 0.9 0.6% of dataset samples above outlier threshold
Additional Outputs
The Outlier Probability Score is calculated by the LoOP algorithm which measures the local deviation of density of a given sample with respect to its neighbors. These outlier scores are directly interpretable as a probability of an object being an outlier (see link for more information).

  Outlier Probability Score 0 1 2 3
150 1.00 1.00 10.00 50.00 100.00
41 0.67 4.50 2.30 1.30 0.30
108 0.50 6.70 2.50 5.80 1.80
109 0.50 7.20 3.60 6.10 2.50
22 0.44 4.60 3.60 1.00 0.20


.. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.070 seconds) .. _sphx_glr_download_checks_gallery_tabular_integrity_plot_outlier_sample_detection.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_outlier_sample_detection.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_outlier_sample_detection.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_