0

I'm looking for a fast way to compute euclidean distance of all values in a array. The Result should be in a new array ordered ascending with the two used "partners" for calculation.

eg:

a = [[2,4,5],[3,2,1],[5,7,2]]

res = euclidean distance(a) ordered ascending with

format: [result, value A, value B] (result is the eu.dist. between value A and value B in array a)

e.g: (not calculated)

res = [[4, 0, 1],[6, 0, 2], [9, 1, 2]]

thin i will calculate the eu.dist in this way

def euclidean(a, b):
    dist = numpy.linalg.norm(a-b)
    return dist 
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Linda
  • 2,375
  • 4
  • 30
  • 33

2 Answers2

2

Try using scipy.spatial.distance.cdist, as given in the answer to this question. Both inputs would be your a array. The only thing is that you won't get the exact format you're looking for - you'll get a matrix with the (i,j) element giving you the required distance instead. Hope this helps.

Community
  • 1
  • 1
Praveen
  • 6,872
  • 3
  • 43
  • 62
  • 1
    I think `pdist` might be better than `cdist` when you just have a single list of inputs points. Alas, I don't have `scipy` installed on this system to test it out myself. – Blckknght Sep 07 '13 at 13:53
  • 1
    @Blckknght `pdist` will be sightly over twice as fast as `cdist` as it only has to calculate the upper triangular portion of the `cdist` array. – Daniel Sep 07 '13 at 19:03
1

The itertools.combinations function should work to get you the various pairs of elements, then you just need to find the distance and sort them:

distances = [[euclidean(points[a], points[b]), a, b]
             for a, b in itertools.combinations(range(len(points)), 2)]
distances.sort() # distance is already first element, so no key function required

Now, with numpy values this may not be the most efficient way to go, but it works.

Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • works fine thanks 2 both! Praveen method = 0.00520396232605 blckknght method = 0.00202894210815 – Linda Sep 08 '13 at 11:17