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.

Alex_6
  • 259
  • 4
  • 16
  • 1
    How did you arrive at the output? Look into Scipy's pdist. – Divakar Aug 19 '19 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 – Alex_6 Aug 19 '19 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 – Alex_6 Aug 19 '19 at 10:08
  • 2
    This should solve/get you closer - https://stackoverflow.com/questions/52030458/. For ext. sources - https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise_distances.html. – Divakar Aug 19 '19 at 10:19

1 Answers1

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])

Jeronimo
  • 2,268
  • 2
  • 13
  • 28