Skip to content

Commit

Permalink
ENH: euclidean_distances: swap X and Y sometimes with float32
Browse files Browse the repository at this point in the history
Signed-off-by: Celelibi <celelibi@gmail.com>
  • Loading branch information
Celelibi committed Jun 25, 2018
1 parent 92060d1 commit 98e88d7
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions sklearn/metrics/pairwise.py
Expand Up @@ -202,6 +202,15 @@ def _euclidean_distances_cast(X, Y, outdtype, Y_norm_squared=None,
The computation is done by blocks to limit additional memory usage.
"""
# For performance reasons, swap X and Y if I got X_norm_squared but not
# Y_norm_squared
if X_norm_squared is not None and Y_norm_squared is None:
swap = True
X, Y = Y, X
X_norm_squared, Y_norm_squared = None, X_norm_squared.T
else:
swap = False

# No more than 10MB of additional memory will be used to cast X and Y to
# float64 and to get the float64 result.
maxmem = 10*1024*1024
Expand Down Expand Up @@ -262,6 +271,9 @@ def _euclidean_distances_cast(X, Y, outdtype, Y_norm_squared=None,
if X is Y and j > i:
distances[j:jpbs, i:ipbs] = d.T

if swap:
distances = distances.T

return distances if squared else np.sqrt(distances, out=distances)


Expand Down

0 comments on commit 98e88d7

Please sign in to comment.