8000 MAINT Parameters validation for sklearn.metrics.pairwise.paired_cosine_distances by Charlie-XIAO · Pull Request #26075 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

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
7 changes: 5 additions & 2 deletions sklearn/metrics/pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,9 @@ def paired_manhattan_distances(X, Y):
return np.abs(diff).sum(axis=-1)


@validate_params(
{"X": ["array-like", "sparse matrix"], "Y": ["array-like", "sparse matrix"]}
)
def paired_cosine_distances(X, Y):
"""
Compute the paired cosine distances between X and Y.
Expand All @@ -1102,10 +1105,10 @@ def paired_cosine_distances(X, Y):

Parameters
----------
X : array-like of shape (n_samples, n_features)
X : {array-like, sparse matrix} of shape (n_samples, n_features)
An array where each row is a sample and each column is a feature.

Y : array-like of shape (n_samples, n_features)
Y : {array-like, sparse matrix} of shape (n_samples, n_features)
An array where each row is a sample and each column is a feature.

Returns
Expand Down
9 changes: 9 additions & 0 deletions sklearn/metrics/tests/test_pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
from sklearn.metrics.pairwise import paired_distances
from sklearn.metrics.pairwise import paired_euclidean_distances
from sklearn.metrics.pairwise import paired_manhattan_distances
from sklearn.metrics.pairwise import paired_cosine_distances
from sklearn.metrics.pairwise import _euclidean_distances_upcast
from sklearn.preprocessing import normalize
from sklearn.exceptions import DataConversionWarning
Expand Down Expand Up @@ -1177,6 +1178,14 @@ def test_paired_manhattan_distances():
assert_allclose(D, [1.0, 2.0])


def test_paired_cosine_distances():
# Check the paired manhattan distances computation
X = [[0], [0]]
Y = [[1], [2]]
D = paired_cosine_distances(X, Y)
assert_allclose(D, [0.5, 0.5])


def test_chi_square_kernel():
rng = np.random.RandomState(0)
X = rng.random_sample((5, 4))
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 @@ -209,6 +209,7 @@ def _check_function_param_validation(
"sklearn.metrics.pairwise.haversine_distances",
"sklearn.metrics.pairwise.laplacian_kernel",
"sklearn.metrics.pairwise.linear_kernel",
"sklearn.metrics.pairwise.paired_cosine_distances",
"sklearn.metrics.pairwise.paired_euclidean_distances",
"sklearn.metrics.pairwise.paired_manhattan_distances",
"sklearn.metrics.precision_recall_curve",
Expand Down
0