Here are the original input variables:
A = np.array([[1,1,1,1],[2,2,2,2]])
B = np.array([[1,2,3,4],[1,1,1,1],[1,2,1,9]])
A
B
A is a 2x4 array. B is a 3x4 array.
, dist[i,j] i- j- B. , dist 2x3 .

numpy
dist = np.sqrt(np.sum(np.square(A-B)))
, , , A-B , , 2 3 .
A has dimensions 2 x 4
B has dimensions 3 x 4
, A, B, numpy . A , 2 x 1 x 4, . . scipy .
np.newaxis np.reshape. :
A[:,np.newaxis,:] has dimensions 2 x 1 x 4
B has dimensions 3 x 4
np.reshape(A, (2,1,4)) has dimensions 2 x 1 x 4
B has dimensions 3 x 4
, . np.newaxis. , A-B, 2x3x4:
diff = A[:,np.newaxis,:] - B
diff.shape
dist, :
dist = np.sqrt(np.sum(np.square(A[:,np.newaxis,:] - B), axis=2))
dist
, sum axis=2, 2x3x4 ( 0).
, . , , . , numpy 2x3x4 . , a x z B b x z, numpy a x b x z .
, . , , - .

, . . A B - , . , :

:
threeSums = np.sum(np.square(A)[:,np.newaxis,:], axis=2) - 2 * A.dot(B.T) + np.sum(np.square(B), axis=1)
dist = np.sqrt(threeSums)
dist
, , . , , 2x3x4 .
, , threeSums .
np.sum(np.square(A)[:,np.newaxis,:], axis=2) has dimensions 2 x 1
2 * A.dot(B.T) has dimensions 2 x 3
np.sum(np.square(B), axis=1) has dimensions 1 x 3
, , dist 2x3.
.