Skip to content

Commit b29c79d

Browse files
jjerphanogriselthomasjpfan
committed
Remove NaN guard and adapt -0. guard
Co-authored-by: Olivier Grisel <[email protected]> Co-authored-by: Thomas J. Fan <[email protected]>
1 parent 192dd92 commit b29c79d

File tree

2 files changed

+6
-18
lines changed

2 files changed

+6
-18
lines changed

sklearn/metrics/_pairwise_distances_reduction/_argkmin.pyx.tp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -509,15 +509,9 @@ cdef class EuclideanArgKmin{{name_suffix}}(ArgKmin{{name_suffix}}):
509509
self.Y_norm_squared[j + Y_start]
510510
)
511511

512-
sqeuclidean_dist_i_j = (
513-
sqeuclidean_dist_i_j
514-
# Catastrophic cancellation causing -0. and NaN can be present
515-
# in some situations, e.g. when computing d(x_i, y_i) when X is Y.
516-
#
517-
# Guard against -0. Guard against NaN
518-
# v v
519-
if 0. < sqeuclidean_dist_i_j == sqeuclidean_dist_i_j else 0.
520-
)
512+
# Catastrophic cancellation might cause -0. to be present,
513+
# e.g. when computing d(x_i, y_i) when X is Y.
514+
sqeuclidean_dist_i_j = max(0., sqeuclidean_dist_i_j)
521515

522516
heap_push(
523517
values=heaps_r_distances + i * self.k,

sklearn/metrics/_pairwise_distances_reduction/_radius_neighbors.pyx.tp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -513,15 +513,9 @@ cdef class EuclideanRadiusNeighbors{{name_suffix}}(RadiusNeighbors{{name_suffix}
513513
+ self.Y_norm_squared[j]
514514
)
515515

516-
sqeuclidean_dist_i_j = (
517-
sqeuclidean_dist_i_j
518-
# Catastrophic cancellation causing -0. and NaN can be present
519-
# in some situations, e.g. when computing d(x_i, y_i) when X is Y.
520-
#
521-
# Guard against -0. Guard against NaN
522-
# v v
523-
if 0. < sqeuclidean_dist_i_j == sqeuclidean_dist_i_j else 0.
524-
)
516+
# Catastrophic cancellation might cause -0. to be present,
517+
# e.g. when computing d(x_i, y_i) when X is Y.
518+
sqeuclidean_dist_i_j = max(0., sqeuclidean_dist_i_j)
525519

526520
if sqeuclidean_dist_i_j <= self.r_radius:
527521
deref(self.neigh_distances_chunks[thread_num])[i].push_back(sqeuclidean_dist_i_j)

0 commit comments

Comments
 (0)