# KernelDensity

### *class* cuml.neighbors.KernelDensity(, bandwidth=1.0, kernel='gaussian', metric='euclidean', metric_params=None, output_type=None, verbose=False)

Kernel Density Estimation. Computes a non-parametric density estimate
from a finite data sample, smoothing the estimate according to a
bandwidth parameter.

* **Parameters:**
  **bandwidth**
  : The bandwidth of the kernel.

  **kernel**
  : The kernel to use.

  **metric**
  : The distance metric to use.  Note that not all metrics are
    valid with all algorithms. Note that the normalization of the density
    output is correct only for the Euclidean distance metric. Default
    is ‘euclidean’.

  **metric_params**
  : Additional parameters to be passed to the tree for use with the
    metric.

  **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.

  **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.
* **Attributes:**
  **n_features_in_**
  : Number of features seen during fit.

  **bandwidth_**
  : Value of the bandwidth used, either given directly via `bandwidth` or
    estimated with `bandwidth="scott"` or `bandwidth="silverman"`.

### Methods

| [`fit`](#cuml.neighbors.KernelDensity.fit)(self, X[, y, sample_weight])        | Fit the Kernel Density model on the data.                  |
|------------------------------------------------------------------------------------------|------------------------------------------------------------|
| [`sample`](#cuml.neighbors.KernelDensity.sample)(self[, n_samples, random_state]) | Generate random samples from the model.                    |
| [`score`](#cuml.neighbors.KernelDensity.score)(self, X[, y])                     | Compute the total log-likelihood under the model.          |
| [`score_samples`](#cuml.neighbors.KernelDensity.score_samples)(self, X)                  | Compute the log-likelihood of each sample under the model. |

### Examples

```pycon
>>> from cuml.neighbors import KernelDensity
>>> import cupy as cp
>>> rng = cp.random.RandomState(42)
>>> X = rng.random_sample((100, 3))
>>> kde = KernelDensity(kernel='gaussian', bandwidth=0.5).fit(X)
>>> log_density = kde.score_samples(X[:3])
```

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

#### as_sklearn()[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/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 !! -->

#### fit(self, X, y=None, sample_weight=None) → 'KernelDensity'[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/neighbors/kernel_density.pyx#L213)

Fit the Kernel Density model on the data.

* **Parameters:**
  **X**
  : List of n_features-dimensional data points.  Each row
    corresponds to a single data point.

  **y**
  : Ignored.

  **sample_weight**
  : List of sample weights attached to the data X.
* **Returns:**
  self
  : Returns the instance itself.

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

#### *classmethod* from_sklearn(model)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/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/93418baf5/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 !! -->

#### sample(self, n_samples=1, random_state=None)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/neighbors/kernel_density.pyx#L412)

Generate random samples from the model.

Currently, this is implemented only for gaussian and tophat kernels.

* **Parameters:**
  **n_samples**
  : Number of samples to generate.

  **random_state**
  : Determines random number generation used to generate
    random samples.
* **Returns:**
  **X**
  : List of samples.

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

#### score(self, X, y=None) → [float](https://docs.python.org/3/builtins/functions.html#float)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/neighbors/kernel_density.pyx#L391)

Compute the total log-likelihood under the model.

* **Parameters:**
  **X**
  : List of n_features-dimensional data points.  Each row
    corresponds to a single data point.

  **y**
  : Ignored.
* **Returns:**
  **logprob**
  : Total log-likelihood of the data in X. This is normalized to be a
    probability density, so the value will be low for high-dimensional
    data.

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

#### score_samples(self, X)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/neighbors/kernel_density.pyx#L276)

Compute the log-likelihood of each sample under the model.

* **Parameters:**
  **X**
  : An array of points to query.  Last dimension should match dimension
    of training data (n_features).
* **Returns:**
  **density**
  : Log-likelihood of each sample in `X`. These are normalized to be
    probability densities, so values will be low for high-dimensional
    data.

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

#### set_params(\*\*params)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/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 !! -->
