我有两个numpy数组(称为
我想找到每个点在
我希望输出为矩阵D,其中
以下是基于http://nonconditional.com/2014/04/on-the-trick-for-computing-the-squared-euclidian-distances-between-two-sets-of-vectors/的Python代码。
X
和Y
),每一行代表一个点向量。我想找到每个点在
X
中到Y
中每个点的平方欧几里得距离(将其称为“dist”)。我希望输出为矩阵D,其中
D(i,j)
是dist(X(i), Y(j))
。以下是基于http://nonconditional.com/2014/04/on-the-trick-for-computing-the-squared-euclidian-distances-between-two-sets-of-vectors/的Python代码。
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
我正在尝试避免循环(应该吗?),并使用矩阵乘法进行快速计算。
但是,当大数组出现“内存错误”时,我遇到了问题。也许有更好的方法来解决这个问题?