Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a numpy array like:

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

I would like to calculate euclidian distance between each pair of rows.

from scipy.spatial import distance
for i in range(0,a.shape[0]):
    d = [np.sqrt(np.sum((a[i]-a[j])**2)) for j in range(i+1,a.shape[0])]
    print(d)

[1.4142135623730951, 0.0, 1.4142135623730951]

[1.4142135623730951, 2.0]

[1.4142135623730951]

[]

Is there any better pythonic way to do this since i have to run this code on a huge numpy array?

like image 266
Rashmi Singh Avatar asked Nov 29 '22 00:11

Rashmi Singh


1 Answers

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.        ]])
like image 152
comendeiro Avatar answered May 16 '23 06:05

comendeiro