Alex_6
Alex_6

Reputation: 319

How to compute distance from elements of an array in python?

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.

Upvotes: 1

Views: 1409

Answers (1)

Jeronimo
Jeronimo

Reputation: 2387

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

Upvotes: 2

Related Questions