Skip to content

Commit 98e88d7

Browse files
committed
ENH: euclidean_distances: swap X and Y sometimes with float32
Signed-off-by: Celelibi <celelibi@gmail.com>
1 parent 92060d1 commit 98e88d7

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

sklearn/metrics/pairwise.py

+12
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,15 @@ def _euclidean_distances_cast(X, Y, outdtype, Y_norm_squared=None,
202202
203203
The computation is done by blocks to limit additional memory usage.
204204
"""
205+
# For performance reasons, swap X and Y if I got X_norm_squared but not
206+
# Y_norm_squared
207+
if X_norm_squared is not None and Y_norm_squared is None:
208+
swap = True
209+
X, Y = Y, X
210+
X_norm_squared, Y_norm_squared = None, X_norm_squared.T
211+
else:
212+
swap = False
213+
205214
# No more than 10MB of additional memory will be used to cast X and Y to
206215
# float64 and to get the float64 result.
207216
maxmem = 10*1024*1024
@@ -262,6 +271,9 @@ def _euclidean_distances_cast(X, Y, outdtype, Y_norm_squared=None,
262271
if X is Y and j > i:
263272
distances[j:jpbs, i:ipbs] = d.T
264273

274+
if swap:
275+
distances = distances.T
276+
265277
return distances if squared else np.sqrt(distances, out=distances)
266278

267279

0 commit comments

Comments
 (0)