#### Let's consider the problem of finding the closest restaurant to our current GPS coordinates. Let's assume the current position is given as an (x,y) coordinate, and that we have coordinates of various restaurants stored in a list of positions.¶

In [1]:
import math

def closest(position, positions):
x0, y0 = position
dbest, ibest = None, None
for i, (x,y) in enumerate(positions):
# compute the Euclidean distance
dist = ((x - x0) ** 2) + ((y - y0) ** 2)
dist = math.sqrt(dist)
if dbest is None or dist < dbest:
dbest, ibest = dist, i
return ibest


#### First we'll create a random list of coordinates. To make it realistic, let's create 10 M coordinates.¶

In [2]:
import random

In [3]:
positions = [(random.random(), random.random()) for i in range(10000000)]

In [4]:
for i in range(0, 10):
print(positions[i])

(0.8367067617471916, 0.8681954690987693)
(0.24766284208655387, 0.1430469172954475)
(0.37693240513198545, 0.9491498797122762)
(0.7310683868163543, 0.8645436627528782)
(0.8904739385229614, 0.1420507285593402)
(0.8923964723761243, 0.6737510140986144)
(0.6879572668201521, 0.2008072195186068)
(0.14446217586642773, 0.8613472687511541)
(0.71031134638013, 0.66847510504419)
(0.8751207509656234, 0.8206094040619555)


#### Let's see how long it takes to compute the closest distance to our current coordinates: (0.5,0.5)¶

In [5]:
%timeit p = closest((.5, .5), positions)

6.63 s ± 189 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [6]:
print( positions[closest((.5, .5), positions)] )

(0.49992528656550106, 0.4998538500083588)


#### Now let's try doing something similar with Numpy. Numpy arrays are much more efficient, and so is the method for random number generation.¶

In [7]:
import numpy as np

In [8]:
positions = np.array(positions)

In [9]:
positions.shape

Out[9]:
(10000000, 2)
In [10]:
print(positions[:10,:])

[[0.83670676 0.86819547]
[0.24766284 0.14304692]
[0.37693241 0.94914988]
[0.73106839 0.86454366]
[0.89047394 0.14205073]
[0.89239647 0.67375101]
[0.68795727 0.20080722]
[0.14446218 0.86134727]
[0.71031135 0.66847511]
[0.87512075 0.8206094 ]]


#### Now let's again compute the distances to our position (0.5, 0.5)¶

In [11]:
x, y = positions[:,0], positions[:,1] # x and y are arrays containing the 1st and 2nd cols, respectively.

In [12]:
distances = np.sqrt((x - 0.5)**2 + (y - 0.5)**2)
closest_dist = distances.min()

In [13]:
%%timeit
distances = np.sqrt( (x - 0.5)**2 + (y - 0.5)**2 )

226 ms ± 13 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [14]:
%%timeit
closest_dist = distances.min()

5.13 ms ± 58.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [ ]: