python: fastest way to compute euclidean distance of a vector to every row of a matrix?

左心房为你撑大大i 提交于 2021-02-07 10:53:47

问题


Consider this python code, where I try to compute the eucliean distance of a vector to every row of a matrix. It's very slow compared to the best Julia version I can find using Tullio.jl.

The python version takes 30s but the Julia version only takes 75ms.

I am sure I am not doing the best in Python. Are there faster solutions? Numba and numpy solutions welcome.

import numpy as np

# generate
a = np.random.rand(4000000, 128)

b = np.random.rand(128)

print(a.shape)
print(b.shape)



def lin_norm_ever(a, b):
    return np.apply_along_axis(lambda x: np.linalg.norm(x - b), 1, a)


import time
t = time.time()
res = lin_norm_ever(a, b)
print(res.shape)
elapsed = time.time() - t
print(elapsed)

The Julia verions

using Tullio
function comp_tullio(a, c)
    dist = zeros(Float32, size(a, 2))
    @tullio dist[i] = (c[j] - a[j,i])^2

    dist
end
@time comp_tullio(a, c)

@benchmark comp_tullio(a, c) # 75ms on my computer

回答1:


I would use Numba in this example for best performance. I also added 2 approaches from Divakars linked answer for comparison.

Code

import numpy as np
import numba as nb
from scipy.spatial.distance import cdist

@nb.njit(fastmath=True,parallel=True,cache=True)
def dist_1(mat,vec):
    res=np.empty(mat.shape[0],dtype=mat.dtype)
    for i in nb.prange(mat.shape[0]):
        acc=0
        for j in range(mat.shape[1]):
            acc+=(mat[i,j]-vec[j])**2
        res[i]=np.sqrt(acc)
    return res

#from https://stackoverflow.com/a/52364284/4045774
def dist_2(mat,vec):
    return cdist(mat, np.atleast_2d(vec)).ravel()

#from https://stackoverflow.com/a/52364284/4045774
def dist_3(mat,vec):
    M = mat.dot(vec)
    d = np.einsum('ij,ij->i',mat,mat) + np.inner(vec,vec) -2*M
    return np.sqrt(d)

Timings

#Float64
a = np.random.rand(4000000, 128)
b = np.random.rand(128)
%timeit dist_1(a,b)
#122 ms ± 3.86 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit dist_2(a,b)
#484 ms ± 3.02 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit dist_3(a,b)
#432 ms ± 14.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

#Float32
a = np.random.rand(4000000, 128).astype(np.float32)
b = np.random.rand(128).astype(np.float32)
%timeit dist_1(a,b)
#68.6 ms ± 414 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit dist_2(a,b)
#2.2 s ± 32.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
#looks like there is a costly type-casting to float64
%timeit dist_3(a,b)
#228 ms ± 8.13 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)


来源:https://stackoverflow.com/questions/63517352/python-fastest-way-to-compute-euclidean-distance-of-a-vector-to-every-row-of-a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!