Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding 3d distances using an inbuilt function in python

I have 6 lists storing x,y,z coordinates of two sets of positions (3 lists each). I want to calculate the distance between each point in both sets. I have written my own distance function but it is slow. One of my lists has about 1 million entries. I have tried cdist, but it produces a distance matrix and I do not understand what it means. Is there another inbuilt function that can do this?

like image 723
Abhinav Kumar Avatar asked Nov 25 '13 04:11

Abhinav Kumar


People also ask

How to calculate distance between two points in Python?

To calculate the Euclidean distance between the points (x1, y1) and (x2, y2) you can use the formula: For example, the distance between points (2, 3) and (5, 7) is 5. Note that the above formula can be extended to n-dimensions. Now that we know how the distance between two points is computed mathematically, we can proceed to compute it in Python.

How to calculate Euclidean distance in Python?

Euclidean Distance is a distance between two points in space that can be measured with the help of the Pythagorean formula. The formula is shown below: square root of [ (x-a)^2 + (y-b)^2 + (z-c)^2 ]. To calculate the Euclidean Distance between two coordinate points we will be making use of the numpy module in python.

How to plot 3 dimensional line graph in Python 3?

Plotting 3-D Lines and Points Graph with lines and point are the simplest 3 dimensional graph. ax.plot3d and ax.scatter are the function to plot line and point graph respectively. Example 1: 3 dimensional line graph Python3

How to find the Euclidean distance between two points in NumPy?

The Euclidean distance is equivalent to the l2 norm of the difference between the two points which can be calculated in numpy using the numpy.linalg.norm () function. We get the same result as above. Note that, here, we pass the difference between points a and b as a numpy array to the the np.linalg.norm () function.


2 Answers

If possible, use the numpy module to handle this kind of things. It is a lot more efficient than using regular python lists.

I am interpreting your problem like this

  1. You have two sets of points
  2. Both sets have the same number of points (N)
  3. Point k in set 1 is related to point k in set 2. If each point is the coordinate of some object, I am interpreting it as set 1 containing the initial point and set 2 the point at some other time t.
  4. You want to find the distance d(k) = dist(p1(k), p2(k)) where p1(k) is point number k in set 1 and p2(k) is point number k in set 2.

Assuming that your 6 lists are x1_coords, y1_coords, z1_coords and x2_coords, y2_coords, z2_coords respectively, then you can calculate the distances like this

import numpy as np
p1 = np.array([x1_coords, y1_coords, z1_coords])
p2 = np.array([x2_coords, y2_coords, z2_coords])

squared_dist = np.sum((p1-p2)**2, axis=0)
dist = np.sqrt(squared_dist)

The distance between p1(k) and p2(k) is now stored in the numpy array as dist[k].

As for speed: On my laptop with a "Intel(R) Core(TM) i7-3517U CPU @ 1.90GHz" the time to calculate the distance between two sets of points with N=1E6 is 45 ms.

like image 200
Hannes Ovrén Avatar answered Nov 15 '22 16:11

Hannes Ovrén


Although this solution uses numpy, np.linalg.norm could be another solution.

Say you have one point p0 = np.array([1,2,3]) and a second point p1 = np.array([4,5,6]). Then the quickest way to find the distance between the two would be:

dist = np.linalg.norm(p0 - p1)
like image 36
azizbro Avatar answered Nov 15 '22 15:11

azizbro