在numpy中找到n点到m点之间的平方距离 [英] Finding squared distances beteen n points to m points in numpy
问题描述
我有 2 个 numpy 数组(比如 X 和 Y),每行代表一个点向量.
我想找到 X 中每个点到 Y 中每个点之间的平方欧几里德距离(将称为dist").
我希望输出是矩阵 D,其中 D(i,j) 是 dist(X(i) , Y(j)).
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)).
我有以下 python 代码基于:http://nonconditional.com/2014/04/on-the-trick-for-computing-the-squared-euclidian-distances-between-two-sets-向量/
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
我试图避免循环(我应该这样做吗?)并使用矩阵 mult 以进行快速计算.但是我在大型阵列上遇到了内存错误"的问题.也许有更好的方法来做到这一点?
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?
推荐答案
Scipy 拥有 cdist
功能完全符合您的要求:
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.
这篇关于在numpy中找到n点到m点之间的平方距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!