File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1212
1313from ..base import BaseEstimator
1414
15+ def all_pairs_l2_distance_squared (A , B , B_norm_squared = None ):
16+ """
17+ Returns the squared l2 norms of the differences between rows of A and B.
18+
19+ Parameters
20+ ----------
21+ A: array, [n_rows_A, n_cols]
22+
23+ B: array, [n_rows_B, n_cols]
24+
25+ B_norm_squared: array [n_rows_B], or None
26+ pre-computed (B**2).sum(axis=1)
27+
28+ Returns
29+ -------
30+
31+ array [n_rows_A, n_rows_B]
32+ entry [i,j] is ((A[i] - B[i])**2).sum(axis=1)
33+
34+ """
35+ if B_norm_squared is None :
36+ B_norm_squared = (B ** 2 ).sum (axis = 1 )
37+ if A is B :
38+ A_norm_squared = B_norm_squared
39+ else :
40+ A_norm_squared = (A ** 2 ).sum (axis = 1 )
41+ return (B_norm_squared + A_norm_squared .reshape ((A .shape [0 ],1 )) - 2 * np .dot (A , B .T ))
42+
1543################################################################################
1644# Initialisation heuristic
1745
You can’t perform that action at this time.
0 commit comments