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:
- bandwidthfloat or {“scott”, “silverman”}, default=1.0
The bandwidth of the kernel.
- kernel{‘gaussian’, ‘tophat’, ‘epanechnikov’, ‘exponential’, ‘linear’, ‘cosine’}, default=’gaussian’
The kernel to use.
- metricstr, default=’euclidean’
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_paramsdict, default=None
Additional parameters to be passed to the tree for use with the metric.
- output_type{None, ‘input’, ‘cupy’, ‘numpy’, ‘cudf’, ‘pandas’}, default=None
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 for more info.- verboseint or boolean, default=False
Sets logging level. It must be one of
cuml.common.logger.level_*. See Verbosity Levels for more info.
- Attributes:
- n_features_in_int
Number of features seen during fit.
- bandwidth_float
Value of the bandwidth used, either given directly via
bandwidthor estimated withbandwidth="scott"orbandwidth="silverman".
Methods
fit(self, X[, y, sample_weight])Fit the Kernel Density model on the data.
sample(self[, n_samples, random_state])Generate random samples from the model.
score(self, X[, y])Compute the total log-likelihood under the model.
score_samples(self, X)Compute the log-likelihood of each sample under the model.
Examples
>>> 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])
- as_sklearn()[source]#
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.
- fit(
- self,
- X,
- y=None,
- sample_weight=None,
Fit the Kernel Density model on the data.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
List of n_features-dimensional data points. Each row corresponds to a single data point.
- yNone
Ignored.
- sample_weightarray-like of shape (n_samples,), default=None
List of sample weights attached to the data X.
- Returns:
- self
Returns the instance itself.
- classmethod from_sklearn(model)[source]#
Create a cuml estimator from a scikit-learn estimator.
- Parameters:
- modelsklearn.base.BaseEstimator
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_typeof 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.
- get_params(deep=True)[source]#
Returns a dict of all params owned by this class. If the child class has appropriately overridden the
_get_param_namesmethod and does not need anything other than what is there in this method, then it doesn’t have to override this method
- sample(self, n_samples=1, random_state=None)[source]#
Generate random samples from the model.
Currently, this is implemented only for gaussian and tophat kernels.
- Parameters:
- n_samplesint, default=1
Number of samples to generate.
- random_stateint, RandomState instance or None, default=None
Determines random number generation used to generate random samples.
- Returns:
- Xcupy array of shape (n_samples, n_features)
List of samples.
- score(self, X, y=None) float[source]#
Compute the total log-likelihood under the model.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
List of n_features-dimensional data points. Each row corresponds to a single data point.
- yNone
Ignored.
- Returns:
- logprobfloat
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.
- score_samples(self, X)[source]#
Compute the log-likelihood of each sample under the model.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
An array of points to query. Last dimension should match dimension of training data (n_features).
- Returns:
- densityndarray of shape (n_samples,)
Log-likelihood of each sample in
X. These are normalized to be probability densities, so values will be low for high-dimensional data.
- set_params(**params)[source]#
Accepts a dict of params and updates the corresponding ones owned by this class. If the child class has appropriately overridden the
_get_param_namesmethod and does not need anything other than what is, there in this method, then it doesn’t have to override this method