5

I have 3D array as

 A = [[x1 y1 z1]
      [x2 y2 z2]
      [x3 y3 z3]]

I have to find euclidean distance between each points so that I'll get output with only 3 distance between (row0,row1),(row1,row2) and (row0,row2).

I have some code

dist = scipy.spatial.distance.cdist(A,A, 'euclidean')

but it will give distance in matrix form as

dist= [[0  a   b]
       [a  0   c]
       [b  c   0]]

I want results as [a b c].

0

2 Answers 2

6

Consider using scipy.spatial.distance.pdist.

You can do like this.

>>> A = np.array([[1, 2, 3], [4, 5, 6], [10, 20, 30]])
>>> scipy.spatial.distance.pdist(A)
array([  5.19615242,  33.67491648,  28.93095228])

But be careful the order of the output distance is (row0,row1),(row0,row2) and (row1,row2).

Sign up to request clarification or add additional context in comments.

1 Comment

Yep no problem at all.:)
3

You can do something like this:

>>> import numpy as np
>>> from itertools import combinations
>>> A = np.array([[1, 2, 3], [4, 5, 6], [10, 20, 30]])
>>> [np.linalg.norm(a-b) for a, b in combinations(A, 2)]
[5.196152422706632, 33.674916480965472, 28.930952282978865]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.