Finding squared distances beteen n points to m points in numpy

梦想与她 提交于 2021-02-19 06:23:06

问题


I have 2 numpy arrays(say X and Y) which each row represents a point vector.
I want to find the squared euclidean distances(will call this 'dist') between each point in X to each point in Y.
I want to the output to be a matrix D where D(i,j) is dist(X(i) , Y(j)).

I have the following python code based on : http://nonconditional.com/2014/04/on-the-trick-for-computing-the-squared-euclidian-distances-between-two-sets-of-vectors/

def get_sq_distances(X, Y):
    a = np.sum(np.square(X),axis=1,keepdims=1)
    b = np.ones((1,Y.shape[0]))
    c = a.dot(b)
    a = np.ones((X.shape[0],1))
    b = np.sum(np.square(Y),axis=1,keepdims=1).T
    c += a.dot(b)
    c -= 2*X.dot(Y.T)
    return c

I'm trying to avoid loops(should I?) and to use matrix mult in order to do a fast computation. But I have the problem of "Memory Error" on large arrays. Maybe there is a better way for doing this?


回答1:


Scipy has the cdist function that does exactly what you want:

from scipy.spatial import distance
distance.cdist(X, Y, 'sqeuclidean')

The docs linked above have some good examples.



来源:https://stackoverflow.com/questions/33760643/finding-squared-distances-beteen-n-points-to-m-points-in-numpy

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