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 15, 2018
1 parent 0a1879c commit ac5d1fb
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions sklearn/metrics/pairwise.py
Expand Up @@ -209,6 +209,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

# Maximum number of additional bytes to use to case X and Y to float64.
maxmem = 10*1024*1024
itemsize = np.float64(0).itemsize
Expand Down Expand Up @@ -268,6 +277,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 ac5d1fb

Please sign in to comment.