Skip to content

MAINT Remove scalar manipulation in consine_distances now clip fixed in array-api-compat #31171

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 1 commit into from
Apr 11, 2025
Merged
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
10 changes: 1 addition & 9 deletions sklearn/metrics/pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
_is_numpy_namespace,
_max_precision_float_dtype,
_modify_in_place_if_numpy,
device,
get_namespace,
get_namespace_and_device,
)
Expand Down Expand Up @@ -1168,14 +1167,7 @@ def cosine_distances(X, Y=None):
S = cosine_similarity(X, Y)
S *= -1
S += 1
# TODO: remove the xp.asarray calls once the following is fixed:
# https://github.com/data-apis/array-api-compat/issues/177
device_ = device(S)
S = xp.clip(
S,
xp.asarray(0.0, device=device_, dtype=S.dtype),
xp.asarray(2.0, device=device_, dtype=S.dtype),
)
S = xp.clip(S, 0.0, 2.0)
if X is Y or Y is None:
# Ensure that distances between vectors and themselves are set to 0.0.
# This may not be the case due to floating point rounding errors.
Expand Down