In [3]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
%load_ext autoreload
%autoreload 2
In [4]:
np.random.seed(10)
p, q = (np.random.rand(i, 2) for i in (4, 5))
p_big, q_big = (np.random.rand(i, 80) for i in (100, 120))
print(p, "\n\n", q)
Out[4]:
In [9]:
def naive(p, q):
x = []
for i in range(len(p)):
x.append([])
for j in range(len(q)):
x.append(np.sqrt((p[i][0] - q[i][0]) + (p[i][1] - q[i][1])))
return x
In [10]:
rows, cols = np.indices((p.shape[0], q.shape[0]))
print(rows, end='\n\n')
print(cols)
In [11]:
print(p[rows.ravel()], end='\n\n')
print(q[cols.ravel()])
In [19]:
def with_indices(p, q):
diff = p[rows.ravel()] - q[cols.ravel()]
return np.sqrt(diff.dot(diff.T))
In [20]:
from scipy.spatial.distance import cdist
def scipy_version(p, q):
return cdist(p, q)
In [21]:
def tensor_broadcasting(p, q):
return np.sqrt(np.sum((p[:,np.newaxis,:]-q[np.newaxis,:,:])**2, axis=2))
In [22]:
methods = [naive, with_indices, scipy_version, tensor_broadcasting]
timers = []
for f in methods:
r = %timeit -o f(p_big, q_big)
timers.append(r)
In [23]:
plt.figure(figsize=(10,6))
plt.bar(np.arange(len(methods)), [r.best*1000 for r in timers], log=False) # Set log to True for logarithmic scale
plt.xticks(np.arange(len(methods))+0.2, [f.__name__ for f in methods], rotation=30)
plt.xlabel('Method')
plt.ylabel('Time (ms)')
plt.show()
In [ ]: