计算矩阵中一个点到所有其他点的距离 [英] Calculate Distances Between One Point in Matrix From All Other Points

查看:1052
本文介绍了计算矩阵中一个点到所有其他点的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,我需要实现一个聚类算法.为此,我将需要计算给定输入数据之间的距离.

I am new to Python and I need to implement a clustering algorithm. For that, I will need to calculate distances between the given input data.

考虑以下输入数据-

    [[1,2,8],
     [7,4,2],
     [9,1,7],
     [0,1,5],
     [6,4,3]]

我想在这里实现的是,我想计算与所有其他点的距离[1,2,8],并找到距离最小的点.

What I am looking to achieve here is, I want to calculate distance of [1,2,8] from ALL other points, and find a point where the distance is minimum.

对于其他所有方面,我必须重复此操作.

And I have to repeat this for ALL other points.

我正在尝试使用FOR循环来实现这一点,但是我确信SciPy/NumPy必须具有可以帮助我有效实现此结果的功能.

I am trying to implement this with a FOR loop, but I am sure that SciPy/ NumPy must be having a function which can help me achieve this result efficiently.

我看了网上,但是'pdist'命令无法完成我的工作.

I looked online, but the 'pdist' command could not get my work done.

有人可以引导我吗?

TIA

推荐答案

结合使用np.linalg.norm和广播( numpy外减法),您可以执行以下操作:

Use np.linalg.norm combined with broadcasting (numpy outer subtraction), you can do:

np.linalg.norm(a - a[:,None], axis=-1)

a[:,None]a中插入一个新轴,由于广播,a - a[:,None]然后将逐行进行减法. np.linalg.norm计算最后一个轴上的np.sqrt(np.sum(np.square(...))):

a[:,None] insert a new axis into a, a - a[:,None] will then do a row by row subtraction due to broadcasting. np.linalg.norm calculates the np.sqrt(np.sum(np.square(...))) over the last axis:

a = np.array([[1,2,8],
     [7,4,2],
     [9,1,7],
     [0,1,5],
     [6,4,3]])

np.linalg.norm(a - a[:,None], axis=-1)
#array([[ 0.        ,  8.71779789,  8.1240384 ,  3.31662479,  7.34846923],
#       [ 8.71779789,  0.        ,  6.164414  ,  8.18535277,  1.41421356],
#       [ 8.1240384 ,  6.164414  ,  0.        ,  9.21954446,  5.83095189],
#       [ 3.31662479,  8.18535277,  9.21954446,  0.        ,  7.        ],
#       [ 7.34846923,  1.41421356,  5.83095189,  7.        ,  0.        ]])

元素[0,1][0,2]例如对应于:

np.sqrt(np.sum((a[0] - a[1]) ** 2))
# 8.717797887081348

np.sqrt(np.sum((a[0] - a[2]) ** 2))
# 8.1240384046359608

分别.

这篇关于计算矩阵中一个点到所有其他点的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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