Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion doc/modules/array_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,12 @@ Metrics
- :func:`sklearn.metrics.pairwise.cosine_distances`
- :func:`sklearn.metrics.pairwise.pairwise_distances` (only supports "cosine", "euclidean", "manhattan" and "l2" metrics)
- :func:`sklearn.metrics.pairwise.euclidean_distances` (see :ref:`device_support_for_float64`)
- :func:`sklearn.metrics.pairwise.laplacian_kernel`
- :func:`sklearn.metrics.pairwise.linear_kernel`
- :func:`sklearn.metrics.pairwise.manhattan_distances`
- :func:`sklearn.metrics.pairwise.paired_cosine_distances`
- :func:`sklearn.metrics.pairwise.paired_euclidean_distances`
- :func:`sklearn.metrics.pairwise.pairwise_kernels` (supports all `sklearn.pairwise.PAIRWISE_KERNEL_FUNCTIONS` except :func:`sklearn.metrics.pairwise.laplacian_kernel`)
- :func:`sklearn.metrics.pairwise.pairwise_kernels`
- :func:`sklearn.metrics.pairwise.polynomial_kernel`
- :func:`sklearn.metrics.pairwise.rbf_kernel` (see :ref:`device_support_for_float64`)
- :func:`sklearn.metrics.pairwise.sigmoid_kernel`
Expand Down
2 changes: 2 additions & 0 deletions doc/whats_new/upcoming_changes/array-api/32613.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- :func:`sklearn.metrics.pairwise.laplacian_kernel` now supports array API compatible inputs.
By :user:`Zubair Shakoor <zubairshakoorarbisoft>`.
6 changes: 5 additions & 1 deletion sklearn/metrics/pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 +1672,11 @@ def laplacian_kernel(X, Y=None, gamma=None):
gamma = 1.0 / X.shape[1]

K = -gamma * manhattan_distances(X, Y)
np.exp(K, K) # exponentiate K in-place
xp, _ = get_namespace(X, Y)
if _is_numpy_namespace(xp):
np.exp(K, K) # exponentiate K in-place
else:
K = xp.exp(K)
return K


Expand Down
2 changes: 2 additions & 0 deletions sklearn/metrics/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
cosine_distances,
cosine_similarity,
euclidean_distances,
laplacian_kernel,
linear_kernel,
manhattan_distances,
paired_cosine_distances,
Expand Down Expand Up @@ -2343,6 +2344,7 @@ def check_array_api_metric_pairwise(metric, array_namespace, device, dtype_name)
euclidean_distances: [check_array_api_metric_pairwise],
manhattan_distances: [check_array_api_metric_pairwise],
linear_kernel: [check_array_api_metric_pairwise],
laplacian_kernel: [check_array_api_metric_pairwise],
polynomial_kernel: [check_array_api_metric_pairwise],
rbf_kernel: [check_array_api_metric_pairwise],
root_mean_squared_error: [
Expand Down
3 changes: 2 additions & 1 deletion sklearn/metrics/tests/test_pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ def test_pairwise_parallel(func, metric, kwds, dtype):
(pairwise_distances, "manhattan", {}),
(pairwise_kernels, "polynomial", {"degree": 1}),
(pairwise_kernels, callable_rbf_kernel, {"gamma": 0.1}),
(pairwise_kernels, "laplacian", {"gamma": 0.1}),
],
)
def test_pairwise_parallel_array_api(
Expand Down Expand Up @@ -487,7 +488,7 @@ def test_pairwise_kernels(metric, csr_container):
)
@pytest.mark.parametrize(
"metric",
["rbf", "sigmoid", "polynomial", "linear", "chi2", "additive_chi2"],
["rbf", "sigmoid", "polynomial", "linear", "laplacian", "chi2", "additive_chi2"],
)
def test_pairwise_kernels_array_api(metric, array_namespace, device, dtype_name):
# Test array API support in pairwise_kernels.
Expand Down
Loading