I would take a look at scipy.spatial.distance.cdist
:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cdist.html
import numpy as np
import scipy
a = np.random.normal(size=(10,3))
b = np.random.normal(size=(1,3))
dist = scipy.spatial.distance.cdist(a,b) # pick the appropriate distance metric
dist
for the default distant metric is equivalent to:
np.sqrt(np.sum((a-b)**2,axis=1))
although cdist
is much more efficient for large arrays (on my machine for your size problem, cdist
is faster by a factor of ~35x).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…