Frage

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 
War es hilfreich?

Lösung 2

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.

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top