在numpy中找到n点到m点之间的平方距离

编程入门 行业动态 更新时间:2024-10-24 12:30:42
本文介绍了在numpy中找到n点到m点之间的平方距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有 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 代码基于:nonconditional/2014/04/on-the-trick-for-computing-the-squared-euclidian-distances-between-two-sets-向量/

I have the following python code based on : nonconditional/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点之间的平方距离

本文发布于:2023-07-27 23:13:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1225212.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:点到   距离   中找到   numpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!