Skip to content

Instantly share code, notes, and snippets.

@pinkwerks
Last active April 6, 2016 18:41
Show Gist options
  • Save pinkwerks/9969927 to your computer and use it in GitHub Desktop.
Save pinkwerks/9969927 to your computer and use it in GitHub Desktop.
Nearest neigbor from a point in 3d space
# find nearest neighbor of a point
import numpy as np
from scipy import spatial
# create 10 random points
x, y, z = np.random.rand(3,10)
# format our points for the kdtree function
points = [i for i in zip(x,y,z)]
# make the kdtree with the points
tree = spatial.KDTree(points)
# create a query point
qpoint = np.array([0,0,0])
# make the query, returns distance to point and the index in 'points'
tree.query(qpoint)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment