Fastest Way To Compute Distance Beetween Each Points In Python
In my project I need to compute euclidian distance beetween each points stored in an array. The entry array is a 2D numpy array with 3 columns which are the coordinates(x,y,z) and
Solution 1:
Not sure where you are getting your timings, but you can use scipy.spatial.distance
:
M = np.arange(6000*3, dtype=np.float64).reshape(6000,3)
np_result = calcul2(M)
sp_result = sd.cdist(M.T, M.T) #Scipy usage
np.allclose(np_result, sp_result)
>>> True
Timings:
%timeitcalcul2(M)1000 loops,best of 3:313µsperloop%timeitsd.cdist(M.T,M.T)10000loops,best of 3:86.4µsperloop
Importantly, its also useful to realize that your output is symmetric:
np.allclose(sp_result, sp_result.T)
>>>True
An alternative is to only compute the upper triangular of this array:
%timeit sd.pdist(M.T)
10000 loops, best of 3: 39.1 µs per loop
Edit: Not sure which index you want to zip, looks like you may be doing it both ways? Zipping the other index for comparison:
%timeit sd.pdist(M)
10 loops, best of 3: 135 ms per loop
Still about 10x faster than your current NumPy implementation.
Post a Comment for "Fastest Way To Compute Distance Beetween Each Points In Python"