Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kernel density estimation #4545

Merged
merged 25 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
39c99b0
KDE
RAMitchell Jan 31, 2022
8bd70a6
logsumexp kernel
RAMitchell Feb 1, 2022
2d8f486
cuml updates for lap, label, cluster, and spectral apis
cjnolet Feb 1, 2022
5902b07
Updating raft pin
cjnolet Feb 1, 2022
fc41d8f
Updating copyrights
cjnolet Feb 1, 2022
4e740d5
Merge remote-tracking branch 'rapidsai/branch-22.04' into imp-2204-la…
cjnolet Feb 8, 2022
ec44e7c
Fixing raft::spasrse calls
cjnolet Feb 9, 2022
bb3fad4
Reverting raft pin
cjnolet Feb 9, 2022
4279f0c
Merge branch 'branch-22.04' of https://github.com/rapidsai/cuml into …
RAMitchell Feb 9, 2022
550c6e8
Merge branch 'imp-2204-lap_label_cluster_spectral_updates' of https:/…
RAMitchell Feb 9, 2022
76172c5
Better tests. Sampling.
RAMitchell Feb 11, 2022
fcd5da4
Revert "Merge branch 'imp-2204-lap_label_cluster_spectral_updates' of…
RAMitchell Feb 11, 2022
77d25dc
Merge branch 'branch-22.04' of https://github.com/rapidsai/cuml into …
RAMitchell Feb 11, 2022
1922daa
Style
RAMitchell Feb 11, 2022
3e05e94
Test sample
RAMitchell Feb 14, 2022
ec75da8
Merge branch 'branch-22.04' of https://github.com/rapidsai/cuml into …
RAMitchell Feb 14, 2022
911e50d
Test sample
RAMitchell Feb 15, 2022
548914c
Update docs
RAMitchell Feb 16, 2022
c5737b7
Disable neighbours pickle tests for KDE
RAMitchell Feb 18, 2022
712000d
Copyright
RAMitchell Feb 18, 2022
d8aa953
Address review comments
RAMitchell Feb 21, 2022
50ff76b
Appease flake8
RAMitchell Feb 24, 2022
3a84237
Test kernel ridge inputs
RAMitchell Mar 7, 2022
5e7f5ad
Kernel density types
RAMitchell Mar 7, 2022
0d5a621
Lint
RAMitchell Mar 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ Metrics (regression, classification, and distance)
.. automodule:: cuml.metrics.pairwise_distances
:members:

.. automodule:: cuml.metrics.pairwise_kernels
:members:


Metrics (clustering and manifold learning)
------------------------------------------
.. automodule:: cuml.metrics.trustworthiness
Expand Down Expand Up @@ -335,6 +339,13 @@ Nearest Neighbors Regression
:members:
:noindex:

Kernel Ridge Regression
-----------------------

.. autoclass:: cuml.KernelRidge
:members:


Clustering
==========

Expand Down Expand Up @@ -429,6 +440,12 @@ Nearest Neighbors Regression
.. autoclass:: cuml.neighbors.KNeighborsRegressor
:members:

Kernel Density Estimation
--------------------------------

.. autoclass:: cuml.neighbors.KernelDensity
:members:

Time Series
============

Expand Down
1 change: 1 addition & 0 deletions python/cuml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
from cuml.naive_bayes.naive_bayes import MultinomialNB

from cuml.neighbors.nearest_neighbors import NearestNeighbors
from cuml.neighbors.kernel_density import KernelDensity
from cuml.neighbors.kneighbors_classifier import KNeighborsClassifier
from cuml.neighbors.kneighbors_regressor import KNeighborsRegressor

Expand Down
33 changes: 18 additions & 15 deletions python/cuml/kernel_ridge/kernel_ridge.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ class KernelRidge(Base, RegressorMixin):
in `cuml.metrics.PAIRWISE_KERNEL_FUNCTIONS` or "precomputed".
If `kernel` is "precomputed", X is assumed to be a kernel matrix.
`kernel` may be a callable numba device function. If so, is called on
each pair of instances (rows) and the resulting value recorded. The
callable should take two rows from X as input and return the
corresponding kernel value as a single number.
each pair of instances (rows) and the resulting value recorded.
gamma : float, default=None
Gamma parameter for the RBF, laplacian, polynomial, exponential chi2
and sigmoid kernels. Interpretation of the default value is left to
Expand Down Expand Up @@ -149,8 +147,10 @@ class KernelRidge(Base, RegressorMixin):
verbose : int or boolean, default=False
Sets logging level. It must be one of `cuml.common.logger.level_*`.
See :ref:`verbosity-levels` for more info.

Attributes
----------

dual_coef_ : ndarray of shape (n_samples,) or (n_samples, n_targets)
Representation of weight vector(s) in kernel space
X_fit_ : ndarray of shape (n_samples, n_features)
Expand Down Expand Up @@ -271,18 +271,21 @@ class KernelRidge(Base, RegressorMixin):
return self

def predict(self, X):
"""Predict using the kernel ridge model.
Parameters
----------
X : array-like of shape (n_samples, n_features)
Samples. If kernel == "precomputed" this is instead a
precomputed kernel matrix, shape = [n_samples,
n_samples_fitted], where n_samples_fitted is the number of
samples used in the fitting for this estimator.
Returns
-------
C : array of shape (n_samples,) or (n_samples, n_targets)
Returns predicted values.
"""
Predict using the kernel ridge model.

Parameters
----------
X : array-like of shape (n_samples, n_features)
Samples. If kernel == "precomputed" this is instead a
precomputed kernel matrix, shape = [n_samples,
n_samples_fitted], where n_samples_fitted is the number of
samples used in the fitting for this estimator.

Returns
-------
C : array of shape (n_samples,) or (n_samples, n_targets)
Returns predicted values.
"""
X_m, _, _, _ = input_to_cuml_array(
X, check_dtype=[np.float32, np.float64])
Expand Down
12 changes: 9 additions & 3 deletions python/cuml/metrics/pairwise_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import numpy as np
import cuml.internals
from cuml.metrics import pairwise_distances
from cuml.common.input_utils import input_to_cupy_array


def linear_kernel(X, Y):
Expand Down Expand Up @@ -191,7 +192,8 @@ def evaluate_pairwise_kernels(X, Y, K):
@cuml.internals.api_return_array(get_output_type=True)
def pairwise_kernels(X, Y=None, metric="linear", *,
filter_params=False, convert_dtype=True, **kwds):
"""Compute the kernel between arrays X and optional array Y.
"""
Compute the kernel between arrays X and optional array Y.
This method takes either a vector array or a kernel matrix, and returns
a kernel matrix. If the input is a vector array, the kernels are
computed. If the input is a kernel matrix, it is returned instead.
Expand All @@ -203,6 +205,7 @@ def pairwise_kernels(X, Y=None, metric="linear", *,
Valid values for metric are:
['additive_chi2', 'chi2', 'linear', 'poly', 'polynomial', 'rbf',
'laplacian', 'sigmoid', 'cosine']

Parameters
----------
X : Dense matrix (device or host) of shape (n_samples_X, n_samples_X) or \
Expand Down Expand Up @@ -233,6 +236,7 @@ def pairwise_kernels(X, Y=None, metric="linear", *,
will increase memory used for the method.
**kwds : optional keyword parameters
Any further parameters are passed directly to the kernel function.

Returns
-------
K : ndarray of shape (n_samples_X, n_samples_X) or \
Expand All @@ -241,6 +245,7 @@ def pairwise_kernels(X, Y=None, metric="linear", *,
ith and jth vectors of the given matrix X, if Y is None.
If Y is not None, then K_{i, j} is the kernel between the ith array
from X and the jth array from Y.

Notes
-----
If metric is 'precomputed', Y is ignored and X is returned.
Expand Down Expand Up @@ -272,11 +277,11 @@ def custom_rbf_kernel(x, y, gamma=None):
pairwise_kernels(X, Y, metric=custom_rbf_kernel)

"""
X = cp.asarray(X)
X = input_to_cupy_array(X).array
if Y is None:
Y = X
else:
Y = cp.asarray(Y)
Y = input_to_cupy_array(Y).array
if X.shape[1] != Y.shape[1]:
raise ValueError("X and Y have different dimensions.")

Expand All @@ -292,4 +297,5 @@ def custom_rbf_kernel(x, y, gamma=None):
else:
kwds = _filter_params(
metric, filter_params, **kwds)

return custom_kernel(X, Y, metric, **kwds)
4 changes: 3 additions & 1 deletion python/cuml/neighbors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2019-2021, NVIDIA CORPORATION.
# Copyright (c) 2019-2022, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,8 @@
from cuml.neighbors.nearest_neighbors import kneighbors_graph
from cuml.neighbors.kneighbors_classifier import KNeighborsClassifier
from cuml.neighbors.kneighbors_regressor import KNeighborsRegressor
from cuml.neighbors.kernel_density import (
KernelDensity, VALID_KERNELS, logsumexp_kernel)

VALID_METRICS = {
"brute": set([
Expand Down
Loading