How to calculate euclidean distance between pair of rows of a numpy array

我是研究僧i 提交于 2019-12-01 19:22:39

And for completeness, einsum is often referenced for distance calculations.

a = np.array([[1,0,1,0],
         [1,1,0,0],
         [1,0,1,0],
         [0,0,1,1]])

b = a.reshape(a.shape[0], 1, a.shape[1])

np.sqrt(np.einsum('ijk, ijk->ij', a-b, a-b))

array([[ 0.        ,  1.41421356,  0.        ,  1.41421356],
       [ 1.41421356,  0.        ,  1.41421356,  2.        ],
       [ 0.        ,  1.41421356,  0.        ,  1.41421356],
       [ 1.41421356,  2.        ,  1.41421356,  0.        ]])

In terms of something more "elegant" you could always use scikitlearn pairwise euclidean distance:

from sklearn.metrics.pairwise import euclidean_distances
euclidean_distances(a,a)

having the same output as a single array.

array([[ 0.        ,  1.41421356,  0.        ,  1.41421356],
       [ 1.41421356,  0.        ,  1.41421356,  2.        ],
       [ 0.        ,  1.41421356,  0.        ,  1.41421356],
       [ 1.41421356,  2.        ,  1.41421356,  0.        ]])

I used itertools.combinations together with np.linalg.norm of the difference vector (this is the euclidean distance):

import numpy as np
import itertools
a = np.array([[1,0,1,0],
              [1,1,0,0],
              [1,0,1,0],
              [0,0,1,1]])

print([np.linalg.norm(x[0]-x[1]) for x in itertools.combinations(a, 2)])

For understanding have a look at this example from the docs:
combinations('ABCD', 2) gives AB AC AD BC BD CD. In your case, A, B, C and D are the rows of your matrix a, so the term x[0]-x[1] appearing in the above code is the difference vector of the vectors in the rows of a.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!