找到两个向量之间每对的距离 [英] Find the distance of each pair between two vectors

查看:61
本文介绍了找到两个向量之间每对的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个向量,比如说 x=[2,4,6,7]y=[2,6,7,8] 并且我想要找到每个对应对之间的欧几里德距离或任何其他实现的距离(例如来自 scipy).那将是dist=[0, 2, 1, 1].

I have two vectors, let's say x=[2,4,6,7] and y=[2,6,7,8] and I want to find the euclidean distance, or any other implemented distance (from scipy for example), between each corresponding pair. That will be dist=[0, 2, 1, 1].

当我尝试

dist = scipy.spatial.distance.cdist(x,y, metric='sqeuclidean')

dist = [scipy.spatial.distance.cdist(x,y, metric='sqeuclidean') for x,y in zip(x,y)]

我明白了

ValueError: XA must be a 2-dimensional array.

我应该如何计算 dist 以及为什么我必须为此目的重塑数据?

How am I supposed to calculate dist and why do I have to reshape data for that purpose?

推荐答案

cdist 不计算对应对之间的距离列表,而是计算所有对之间的距离矩阵.

cdist does not compute the list of distances between corresponding pairs, but the matrix of distances between all pairs.

np.linalg.norm((np.asarray(x)-np.asarray(y))[:, None], axis=1)

id 通常是如何为 n 维点之间的欧几里得距离写这个的;但如果你只处理一维点,绝对差异,如 elpres 建议的那样会更简单.

Is how id typically write this for the Euclidian distance between n-dimensional points; but if you are only dealing with 1 dimensional points, the absolute difference, as suggested by elpres would be simpler.

这篇关于找到两个向量之间每对的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆