Skip to main content
Code Review

Return to Question

Notice removed Canonical answer required by Community Bot
Bounty Ended with no winning answer by Community Bot
added 2 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Is this the fastest way to find Finding the closest point to a list of points using numpy?

I'm trying to find the closest point (Euclidean distance) from a user inputed-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)

Is this the fastest way to find the closest point to a list of points using numpy?

I'm trying to find the closest point (Euclidean distance) from a user inputed 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)

Finding the closest point to a list of points

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)
Notice added Canonical answer required by noɥʇʎԀʎzɐɹƆ
Bounty Started worth 50 reputation by noɥʇʎԀʎzɐɹƆ
edited tags
Link
200_success
  • 145.5k
  • 22
  • 190
  • 479
Tweeted twitter.com/#!/StackCodeReview/status/354150328129687552
Source Link
dassouki
  • 1.1k
  • 1
  • 10
  • 11

Is this the fastest way to find the closest point to a list of points using numpy?

I'm trying to find the closest point (Euclidean distance) from a user inputed 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)
lang-py

AltStyle によって変換されたページ (->オリジナル) /