1

I have a numpy array where each element is a position, like this :

array([[1, 1, 0. ],
       [2 , 2, 0. ],
       [3 , 3, 1 ]])

And I want to compute the distance between each element. So here, the expected output is :

[0, 1.414213, 1.732050]

The distance is calculated as it follows : For the first element it is 0 because ther isn't any element before. For the second it is sqrt((2 - 1)**2 + (2 - 1)**2 + (0 - 0)**2)) and so on

However, there is a lot of elements (around some thousands), and this operation is repeated multiple times. So I need a way to execute this very fast.

I was wondering if there is a library (or even better a numpy function) that could solve my problem. I used cdist from scipy.spatial.distance before, but it doesn't work anymore (I have problems with scipy dependencies), so I'm searching for another way.

4
  • 1
    How did you arrive at the output? Look into Scipy's pdist. Commented Aug 19, 2019 at 10:04
  • For the first element it is 0 because ther isn't any element before. For the second it is sqrt((2 - 1)**2 + (2 - 1)**2 + (0 - 0)**2)) and so on Commented Aug 19, 2019 at 10:06
  • I can't use scipy.spatial as I said. It's a problem about my python configuration, I've made a post about that an hour ago, but I haven't found a way to make it work Commented Aug 19, 2019 at 10:08
  • 2
    This should solve/get you closer - stackoverflow.com/questions/52030458. For ext. sources - scikit-learn.org/stable/modules/generated/…. Commented Aug 19, 2019 at 10:19

1 Answer 1

2

Here's a pure numpy solution. a is your input vector array. You first create the distances component wise, then square them, then sum them over each row, and then get the sqrt of each of those sums.

np.sqrt(np.sum((a[1:] - a[:-1])**2, axis=1))

array([ 1.41421356, 1.73205081])

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

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.