patternpythonMajor
Finding the closest point to a list of points
Viewed 0 times
thepointpointsfindingclosestlist
Problem
I'm trying to find the closest point (Euclidean distance) from a user-inputted point to a list of 50,000 points that I have. Note that the list of points changes all the time. and the closest distance depends on when and where the user clicks on the point.
#find the nearest point from a given point to a large list of points
import numpy as np
def distance(pt_1, pt_2):
pt_1 = np.array((pt_1[0], pt_1[1]))
pt_2 = np.array((pt_2[0], pt_2[1]))
return np.linalg.norm(pt_1-pt_2)
def closest_node(node, nodes):
pt = []
dist = 9999999
for n in nodes:
if distance(node, n) <= dist:
dist = distance(node, n)
pt = n
return pt
a = []
for x in range(50000):
a.append((np.random.randint(0,1000),np.random.randint(0,1000)))
some_pt = (1, 2)
closest_node(some_pt, a)Solution
It will certainly be faster if you vectorize the distance calculations:
There may be some speed to gain, and a lot of clarity to lose, by using one of the dot product functions:
Ideally, you would already have your list of point in an array, not a list, which will speed things up a lot.
def closest_node(node, nodes):
nodes = np.asarray(nodes)
dist_2 = np.sum((nodes - node)**2, axis=1)
return np.argmin(dist_2)There may be some speed to gain, and a lot of clarity to lose, by using one of the dot product functions:
def closest_node(node, nodes):
nodes = np.asarray(nodes)
deltas = nodes - node
dist_2 = np.einsum('ij,ij->i', deltas, deltas)
return np.argmin(dist_2)Ideally, you would already have your list of point in an array, not a list, which will speed things up a lot.
Code Snippets
def closest_node(node, nodes):
nodes = np.asarray(nodes)
dist_2 = np.sum((nodes - node)**2, axis=1)
return np.argmin(dist_2)def closest_node(node, nodes):
nodes = np.asarray(nodes)
deltas = nodes - node
dist_2 = np.einsum('ij,ij->i', deltas, deltas)
return np.argmin(dist_2)Context
StackExchange Code Review Q#28207, answer score: 43
Revisions (0)
No revisions yet.