Ryan
Ryan

Reputation: 11

Using Python to find distance and angle of surrounding data points in 3D. Finding straight lines

I am using Ipython Notebook. I am working on a project where I need to look at about 100 data points in 3D space and figure out the distance between each and the angle from one another. I want to see correlations of the data points and ultimately see if there is any structure to the data (a straight line hidden somewhere). I have looked into clustering techniques and hough transforms, but they seem not to give me the result I need. Any ideas are much appreciated.. thanks!

Upvotes: 1

Views: 1421

Answers (2)

TheONP
TheONP

Reputation: 453

The distance is best found using scipy.spatial.distance.pdist() as mentioned in cjohnson318's answer. For a small array of points 'a' defined as:

import numpy as np
a=np.array([[0,0,0],[1,1,1],[4,2,-2],[3,-1,2]])

The distance euclidean distance 'D' between the points can be found as:

from scipy.spatial.distance import pdist, squareform
D = squareform(pdist(a))

In 3d polar notation, you would need 2 angles to define the direction from one point to another. It seems like a Cartesian unit vector giving the direction would likely serve your purpose just as well. These can be found as:

(a-a[:,np.newaxis,:]) / D[...,np.newaxis]

This will include NaN's in the diagonal elements, as there is no vector from a point to itself. If necessary, these can be changed to zeros using np.nan_to_num

If you actually do need the angles, you could get them by applying np.arctan to the components of the unit vector.

Upvotes: 0

cjohnson318
cjohnson318

Reputation: 3253

For the first issue of determining the pairwise distance between three dimensional points, you can use scipy.spatial.distance.pdist(). This will generate n(n-1)/2 distances for n points. For the second issue finding the angle between points, that's trickier. It seems so tricky that I don't even really want to think about it; however, to that end, you can use scipy.spatial.distance.cosine(), which will determine the cosine distance between two vectors.

Have you looked at scikits? I've found them very helpful in my work. http://scikit-learn.org/stable/

Upvotes: 1

Related Questions