calculate distance of 2 list of points in numpy

前端 未结 3 543
梦毁少年i
梦毁少年i 2020-12-02 01:44

I have 2 lists of points as numpy.ndarray, each row is the coordinate of a point, like:

a = np.array([[1,0,0],[0,1,0],[0,0,1]])
b = np.array([[1,1,0],[0,1,1]         


        
3条回答
  •  青春惊慌失措
    2020-12-02 01:54

    To compute the squared euclidean distance for each pair of elements off them - x and y, we need to find :

    (Xik-Yjk)**2 = Xik**2 + Yjk**2 - 2*Xik*Yjk
    

    and then sum along k to get the distance at coressponding point as dist(Xi,Yj).

    Using associativity, it reduces to :

    dist(Xi,Yj) = sum_k(Xik**2) + sum_k(Yjk**2) - 2*sum_k(Xik*Yjk)
    

    Bringing in matrix-multiplication for the last part, we would have all the distances, like so -

    dist = sum_rows(X^2), sum_rows(Y^2), -2*matrix_multiplication(X, Y.T)
    

    Hence, putting into NumPy terms, we would end up with the euclidean distances for our case with a and b as the inputs, like so -

    np.sqrt((a**2).sum(1)[:,None] + (b**2).sum(1) - 2*a.dot(b.T))
    

    Leveraging np.einsum, we could replace the first two summation-reductions with -

    np.einsum('ij,ij->i',a,a)[:,None] + np.einsum('ij,ij->i',b,b) 
    

    More info could be found on eucl_dist package's wiki page (disclaimer: I am its author).

提交回复
热议问题