# LocalOutlierFactor

### *class* cuml.neighbors.LocalOutlierFactor(, n_neighbors=20, algorithm='auto', metric='minkowski', p=2, algo_params=None, metric_params=None, contamination='auto', novelty=False, verbose=False, output_type=None)[[source]](https://github.com/NVIDIA/cuml/blob/e8a8ca11d/python/cuml/cuml/neighbors/local_outlier_factor.py#L46)

Unsupervised outlier detection using the Local Outlier Factor (LOF).

The anomaly score of each sample is called the Local Outlier Factor. It
measures the local deviation of the density of a given sample with
respect to its neighbors. The local density is estimated from the
distances to the `n_neighbors` nearest neighbors, and samples that have
a substantially lower density than their neighbors are considered
outliers.

* **Parameters:**
  **n_neighbors**
  : Number of neighbors to use. If it is larger than the number of
    samples provided, all other samples are used.

  **algorithm**
  : The query algorithm to use, see
    [`NearestNeighbors`](../cuml.neighbors.NearestNeighbors/index.html.md#cuml.neighbors.NearestNeighbors) for the valid options.

  **metric**
  : Distance metric to use, see [`NearestNeighbors`](../cuml.neighbors.NearestNeighbors/index.html.md#cuml.neighbors.NearestNeighbors)
    for the supported metrics of each algorithm. With the default
    `p=2`, `'minkowski'` is computed as the euclidean distance.

  **p**
  : Parameter for the Minkowski metric. When p = 1, this is equivalent to
    manhattan distance (l1), and euclidean distance (l2) for p = 2. For
    arbitrary p, minkowski distance (lp) is used.

  **algo_params**
  : Used to configure the nearest neighbor algorithm to be used, see
    [`NearestNeighbors`](../cuml.neighbors.NearestNeighbors/index.html.md#cuml.neighbors.NearestNeighbors).

  **metric_params**
  : Additional keyword arguments for the metric function. The only
    supported key is `'p'` with the `'minkowski'` and `'lp'`
    metrics, where it takes precedence over the `p` parameter. Other
    keys raise a `ValueError`.

  **contamination**
  : The proportion of outliers in the data set, used to define the
    threshold on the scores of the samples.
    - if `'auto'`, the threshold is determined as in the original
      paper (`offset_ = -1.5`).
    - if a float, it should be in the range (0, 0.5].

  **novelty**
  : By default, LocalOutlierFactor is only meant to be used for outlier
    detection (`novelty=False`), and only `fit_predict` is available.
    Set to True to use it for novelty detection. In that case `predict`,
    `decision_function` and `score_samples` should only be used on
    new unseen data and not on the training set.

  **verbose**
  : Sets logging level. It must be one of `cuml.common.logger.level_*`.
    See [Verbosity Levels](../../cuml/index.html.md#verbosity-levels) for more info.

  **output_type**
  : Return results and set estimator attributes to the indicated output
    type. If None, the output type set at the module level
    (`cuml.global_settings.output_type`) will be used. See
    [Output Data Type Configuration](../../cuml/index.html.md#output-data-type-configuration) for more info.
* **Attributes:**
  **negative_outlier_factor_**
  : The opposite LOF of the training samples. The higher, the more
    normal. Inliers tend to have a score close to -1, while outliers tend
    to have a lower score.

  **n_neighbors_**
  : The actual number of neighbors used for the neighbor queries.

  **offset_**
  : Offset used to obtain binary labels from the raw scores. Samples with
    a `negative_outlier_factor_` below `offset_` are detected as
    outliers.

  **effective_metric_**
  : The effective metric used for the distance computation.

  **n_samples_fit_**
  : The number of samples in the fitted data.

  **n_features_in_**
  : Number of features seen during [`fit`](#cuml.neighbors.LocalOutlierFactor.fit).

### Methods

| [`decision_function`](#cuml.neighbors.LocalOutlierFactor.decision_function)(X)   | Shifted opposite of the Local Outlier Factor of X.               |
|-------------------------------------------------------------------------|------------------------------------------------------------------|
| [`fit`](#cuml.neighbors.LocalOutlierFactor.fit)(X[, y])            | Fit the local outlier factor detector from the training dataset. |
| [`fit_predict`](#cuml.neighbors.LocalOutlierFactor.fit_predict)(X[, y])    | Fit the model to the training set X and return the labels.       |
| [`predict`](#cuml.neighbors.LocalOutlierFactor.predict)(X)             | Predict the labels (1 inlier, -1 outlier) of X according to LOF. |
| [`score_samples`](#cuml.neighbors.LocalOutlierFactor.score_samples)(X)       | Opposite of the Local Outlier Factor of X.                       |

### Notes

The neighbor search runs in single precision, also for float64 inputs,
and the scores are returned as float32. With the euclidean metric the
distances come from the expanded form of the distance, which loses
accuracy when the samples are far from the origin compared to the
distances between them. On such data the neighbors, and so the scores,
can differ from scikit-learn. Centering the features before fitting
reduces this effect.

When several training samples are at the same distance from a query,
the selected neighbors can differ from scikit-learn, which changes the
score of the samples around the tie.

For additional docs, see [scikitlearn’s LocalOutlierFactor](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.LocalOutlierFactor.html).

### Examples

```python
>>> import cupy as cp
>>> from cuml.neighbors import LocalOutlierFactor

>>> X = cp.array([[-1.1], [0.2], [101.1], [0.3]], dtype=cp.float32)
>>> lof = LocalOutlierFactor(n_neighbors=2)
>>> lof.fit_predict(X)
array([ 1,  1, -1,  1])
```

<!-- !! processed by numpydoc !! -->

#### as_sklearn()[[source]](https://github.com/NVIDIA/cuml/blob/e8a8ca11d/python/cuml/cuml/internals/interop.py#L206)

Convert this estimator into an equivalent scikit-learn (or scikit-learn
extension) estimator.

* **Returns:**
  sklearn.base.BaseEstimator
  : A scikit-learn compatible estimator instance that mirrors the trained
    state of the current estimator.

<!-- !! processed by numpydoc !! -->

#### decision_function(X)[[source]](https://github.com/NVIDIA/cuml/blob/e8a8ca11d/python/cuml/cuml/neighbors/local_outlier_factor.py#L372)

Shifted opposite of the Local Outlier Factor of X.

Only available when `novelty=True`. The shift offset allows a zero
threshold for being an outlier.

* **Parameters:**
  **X**
  : The query samples.
* **Returns:**
  **scores**
  : The shifted opposite of the Local Outlier Factor of each sample.
    Negative scores represent outliers, positive scores represent
    inliers.

<!-- !! processed by numpydoc !! -->

#### fit(X, y=None) → [LocalOutlierFactor](#cuml.neighbors.LocalOutlierFactor)[[source]](https://github.com/NVIDIA/cuml/blob/e8a8ca11d/python/cuml/cuml/neighbors/local_outlier_factor.py#L245)

Fit the local outlier factor detector from the training dataset.

* **Parameters:**
  **X**
  : Training data.

  **y**
  : Not used, present for API consistency.
* **Returns:**
  **self**
  : The fitted local outlier factor detector.

<!-- !! processed by numpydoc !! -->

#### fit_predict(X, y=None)[[source]](https://github.com/NVIDIA/cuml/blob/e8a8ca11d/python/cuml/cuml/neighbors/local_outlier_factor.py#L322)

Fit the model to the training set X and return the labels.

Only available when `novelty=False`.

* **Parameters:**
  **X**
  : Training data.

  **y**
  : Not used, present for API consistency.
* **Returns:**
  **labels**
  : 1 for inliers, -1 for outliers.

<!-- !! processed by numpydoc !! -->

#### *classmethod* from_sklearn(model)[[source]](https://github.com/NVIDIA/cuml/blob/e8a8ca11d/python/cuml/cuml/internals/interop.py#L222)

Create a cuml estimator from a scikit-learn estimator.

* **Parameters:**
  **model**
  : A compatible scikit-learn (or scikit-learn extension) estimator.
* **Returns:**
  cls
  : A new instance of this cuml estimator class that mirrors the
    state of the input estimator.

### Notes

`output_type` of the estimator is set to “numpy” by default, as these
cannot be inferred from training arguments. If something different is
required, then please use cuml’s output_type configuration utilities.

<!-- !! processed by numpydoc !! -->

#### get_params(deep=True)[[source]](https://github.com/NVIDIA/cuml/blob/e8a8ca11d/python/cuml/cuml/internals/base.py#L191)

Returns a dict of all params owned by this class. If the child class
has appropriately overridden the `_get_param_names` method and does not
need anything other than what is there in this method, then it doesn’t
have to override this method

<!-- !! processed by numpydoc !! -->

#### kneighbors(self, X=None, n_neighbors=None, return_distance=True, , two_pass_precision=False)[[source]](https://github.com/NVIDIA/cuml/blob/e8a8ca11d/python/cuml/cuml/neighbors/nearest_neighbors.pyx#L701)

Query the GPU index for the k nearest neighbors of column vectors in X.

* **Parameters:**
  **X**
  : Dense matrix with dtype float32 or float64.
    Acceptable formats: CUDA array interface compliant objects like
    CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas
    DataFrame/Series.

  **n_neighbors**
  : Number of neighbors to search. If not provided, the n_neighbors
    from the model instance is used (default=10)

  **return_distance: Boolean**
  : If False, distances will not be returned

  **two_pass_precision**
  : When set to True, a slow second pass will be used to improve the
    precision of results returned for searches using L2-derived
    metrics. FAISS uses the Euclidean distance decomposition trick to
    compute distances in this case, which may result in numerical
    errors for certain data. In particular, when several samples
    are close to the query sample (relative to typical inter-sample
    distances), numerical instability may cause the computed distance
    between the query and itself to be larger than the computed
    distance between the query and another sample. As a result, the
    query is not returned as the nearest neighbor to itself.  If this
    flag is set to true, distances to the query vectors will be
    recomputed with high precision for all retrieved samples, and the
    results will be re-sorted accordingly. Note that for large values
    of k or large numbers of query vectors, this correction becomes
    impractical in terms of both runtime and memory. It should be used
    with care and only when strictly necessary (when precise results
    are critical and samples may be tightly clustered).
* **Returns:**
  **distances**
  : The distances of the k-nearest neighbors for each column vector
    in X

  **indices**
  : The indices of the k-nearest neighbors for each column vector in X

<!-- !! processed by numpydoc !! -->

#### kneighbors_graph(self, X=None, n_neighbors=None, mode='connectivity')[[source]](https://github.com/NVIDIA/cuml/blob/e8a8ca11d/python/cuml/cuml/neighbors/nearest_neighbors.pyx#L942)

Find the k nearest neighbors of column vectors in X and return as
a sparse matrix in CSR format.

* **Parameters:**
  **X**
  : Dense matrix with dtype float32 or float64.
    Acceptable formats: CUDA array interface compliant objects like
    CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas
    DataFrame/Series.

  **n_neighbors**
  : Number of neighbors to search. If not provided, the n_neighbors
    from the model instance is used

  **mode**
  : Values in connectivity matrix: ‘connectivity’ returns the
    connectivity matrix with ones and zeros, ‘distance’ returns the
    edges as the distances between points with the requested metric.
* **Returns:**
  **A**
  : n_samples_fit is the number of samples in the fitted data where
    A[i, j] is assigned the weight of the edge that connects i to j.
    Values will either be ones/zeros or the selected distance metric.
    Return types are either cupy’s CSR sparse graph (device) or
    numpy’s CSR sparse graph (host)

<!-- !! processed by numpydoc !! -->

#### predict(X)[[source]](https://github.com/NVIDIA/cuml/blob/e8a8ca11d/python/cuml/cuml/neighbors/local_outlier_factor.py#L395)

Predict the labels (1 inlier, -1 outlier) of X according to LOF.

Only available when `novelty=True`.

* **Parameters:**
  **X**
  : The query samples.
* **Returns:**
  **labels**
  : 1 for inliers, -1 for outliers.

<!-- !! processed by numpydoc !! -->

#### score_samples(X)[[source]](https://github.com/NVIDIA/cuml/blob/e8a8ca11d/python/cuml/cuml/neighbors/local_outlier_factor.py#L345)

Opposite of the Local Outlier Factor of X.

Only available when `novelty=True`. The argument X is supposed to
contain new data: the samples in X are not considered in the
neighborhood of any point, so the scores of the training samples are
available through `negative_outlier_factor_` instead.

* **Parameters:**
  **X**
  : The query samples.
* **Returns:**
  **scores**
  : The opposite of the Local Outlier Factor of each sample. The
    lower, the more abnormal.

<!-- !! processed by numpydoc !! -->

#### set_params(\*\*params)[[source]](https://github.com/NVIDIA/cuml/blob/e8a8ca11d/python/cuml/cuml/internals/base.py#L200)

Accepts a dict of params and updates the corresponding ones owned by
this class. If the child class has appropriately overridden the
`_get_param_names` method and does not need anything other than what is,
there in this method, then it doesn’t have to override this method

<!-- !! processed by numpydoc !! -->
