Skip to content

MAINT Parameters validation for additive_chi2_kernel #25424

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

Merged
merged 2 commits into from
Jan 24, 2023
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
6 changes: 3 additions & 3 deletions sklearn/metrics/pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from ..utils._mask import _get_mask
from ..utils.parallel import delayed, Parallel
from ..utils.fixes import sp_version, parse_version
from ..utils._param_validation import validate_params

from ._pairwise_distances_reduction import ArgKmin
from ._pairwise_fast import _chi2_kernel_fast, _sparse_manhattan
Expand Down Expand Up @@ -1403,6 +1404,7 @@ def cosine_similarity(X, Y=None, dense_output=True):
return K


@validate_params({"X": ["array-like"], "Y": ["array-like", None]})
def additive_chi2_kernel(X, Y=None):
"""Compute the additive chi-squared kernel between observations in X and Y.

Expand All @@ -1423,7 +1425,7 @@ def additive_chi2_kernel(X, Y=None):
X : array-like of shape (n_samples_X, n_features)
A feature array.

Y : ndarray of shape (n_samples_Y, n_features), default=None
Y : array-like of shape (n_samples_Y, n_features), default=None
An optional second feature array. If `None`, uses `Y=X`.

Returns
Expand Down Expand Up @@ -1451,8 +1453,6 @@ def additive_chi2_kernel(X, Y=None):
International Journal of Computer Vision 2007
https://hal.archives-ouvertes.fr/hal-00171412/document
"""
if issparse(X) or issparse(Y):
raise ValueError("additive_chi2 does not support sparse matrices.")
X, Y = check_pairwise_arrays(X, Y)
if (X < 0).any():
raise ValueError("X contains negative values.")
Expand Down
8 changes: 0 additions & 8 deletions sklearn/metrics/tests/test_pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,6 @@ def test_pairwise_kernels(metric):
Y_sparse = csr_matrix(Y)
if metric in ["chi2", "additive_chi2"]:
# these don't support sparse matrices yet
with pytest.raises(ValueError):
pairwise_kernels(X_sparse, Y=Y_sparse, metric=metric)
return
K1 = pairwise_kernels(X_sparse, Y=Y_sparse, metric=metric)
assert_allclose(K1, K2)
Expand Down Expand Up @@ -1231,12 +1229,6 @@ def test_chi_square_kernel():
with pytest.raises(ValueError):
chi2_kernel([[0, 1]], [[0.2, 0.2, 0.6]])

# sparse matrices
with pytest.raises(ValueError):
chi2_kernel(csr_matrix(X), csr_matrix(Y))
with pytest.raises(ValueError):
additive_chi2_kernel(csr_matrix(X), csr_matrix(Y))


@pytest.mark.parametrize(
"kernel",
Expand Down
1 change: 1 addition & 0 deletions sklearn/tests/test_public_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def _check_function_param_validation(
"sklearn.metrics.median_absolute_error",
"sklearn.metrics.multilabel_confusion_matrix",
"sklearn.metrics.mutual_info_score",
"sklearn.metrics.pairwise.additive_chi2_kernel",
"sklearn.metrics.r2_score",
"sklearn.metrics.roc_curve",
"sklearn.metrics.zero_one_loss",
Expand Down